错成这样,算了先把错误答案记录,再好好研究自己的不足
错成这样,算了先把错误答案记录,再好好研究自己的缺少的知识点
自己做的:
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var tet=document.getElementById('txt1').value; //获取第一个输入框的值
var ata=document.getElmentById('txt2').value; //获取第二个输入框的值
var btb=document.getElementById('select').value; //获取选择框的值
swtich(btb){
case1:
document.write(tet+ata);
break;
case2;
document.write(tet-ata);
break;
case3:
document.write(tet*ata);
break;
case4;
document.write(tet/ata);
}
document.getElementById('fruit').value; //设置结果输入框的值
}
count();
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick='count()'/> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
</body>
</html>
答案:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<script type="text/javascript">
function count(){
var txt1 = parseInt( document.getElementById('txt1').value);//获取第一个输入框的值
var txt2 = parseInt( document.getElementById('txt2').value);//获取第二个输入框的值
var select = document.getElementById('select').value;//获取选择框的值
var result = '';
switch (select)
{
case '+':
result = txt1 + txt2;
break;
case '-':
result = txt1 - txt2;
break;
case '*':
result = txt1 * txt2;
break;
case '/':
result = txt1 / txt2;
break;
}
document.getElementById('fruit').value = result;//设置结果输入框的值
}
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick = "count()" />
<input type='text' id='fruit' />
</body>
</html>
发现自己好几个地方根本就没有想到过:
1.var result = ''; 记得上章做编程练习的时候是有个地方设置成var arr1 = []; 这次我又犯这样的错误,根本没有想到,想问下大神,这种用符号设置的最常出现在什么地方?
2.还有提示有这个 parseInt()函数可解析一个字符串,并返回一个整数,我都没有想到用到这里使用var txt1 = parseInt( document.getElementById('txt1').value);这个是不是跟return的作用差不多,用了之后就可以直接调用
return是返回值,一般是在函数中使用,document.write是在HTML中打印语句。
3.
parseInt( ) 的定义:
parseInt() 函数可解析一个字符串,并返回一个整数。
parseInt( ) 的语法:
parseInt ( String s , [ int radix ] )
参数 String s ,必需,表示要被解析的字符串。
int radix ,可选,表示要解析的数字的基数。