读书人

请帮忙解释一下代码小弟我看不懂

发布时间: 2012-03-03 15:33:03 作者: rapoo

请帮忙解释一下代码,我看不懂
主要就是有颜色的句子不明白,请帮忙看下,谢谢了


<head>
<title>A simple calculator</title>

<script type = "text/javascript">
<!--

function doit() {
form.input.value = eval(form.input.value)
}



</script>

</head>

<body>
<form name = "form" action = "">

<table border = "1" width = "450" cellspacing = "5"
summary = "This is a calculator used to calculate simple mathematics operations">

<caption>Simple Calculator</caption>

<thead></thead>


<tbody>
<tr>
<td colspan = "3" height = "15">
<input name = "input" type = "text" size = "32"></td>
</tr>

<tr>
<td width = "60" height = "15">
<input name = "one" type = "button" value = " 1 "
onClick = "form.input.value += '1'" /></td>
<td width = "60" height = "15">
<input name = "two" type = "button" value = " 2 "
onClick = "form.input.value += '2'" /></td>
<td width = "60" height = "15">
<input name = "three" type = "button" value = " 3 "
onClick = "form.input.value += '3'" /></td>
<td width = "1" height = "15"></td>
<td width = "60" height = "15">
<input name = "MultiplySign" type = "button" value = " * "
onClick = "form.input.value += '*'" /></td>
</tr>


<tr>
<td width = "60" height = "15">
<input name = "equal" type = "button" value = " = "
onClick = "doit()" /></td>
</tr>

</table>
</form>
</body>



[解决办法]
form.input.value += '1'
相当于在form.input.value = form.input.value + '1'
意思是在原来的基础上加1
[解决办法]
将原先的value后追加(1,2,3,*)
。。。。
[解决办法]
这个form.input.value 指的是这个 <input name = "input" type = "text" size = "32">的value,

按钮的value值只是做显示用,显示在按钮的表面,

当然他的功能,就是onClick里面执行的,把上面那个input的值改变。
------解决方案--------------------


<input name = "one" type = "button" value = " 1 "
onClick = "form.input.value += '1'" /></td>
这个onClick方法所改变的是
<input name = "input" type = "text" size = "32"></td>
这个文本框的值,而form.input.value += '1'意思是在每次按下改按钮时,
<input name = "input" type = "text" size = "32"></td>
改文本框显示的值自动追加1,如原来是1,那么按一下就变11,依次类推其他按钮的方法是一样的功能

读书人网 >JavaScript

热点推荐