为什么全局变量写在head会没用。但是写在body里就可以用?
你知道原因了吗
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 事件</title>
<script type="text/javascript">
var text1=document.getElementById('txt1');
var text2=document.getElementById('txt2');
var text3=document.getElementById('fruit');
var v = document.getElementById("select");
function a(){
var bb = v.selectedIndex; // 选中索引
var aa = v.options[bb].value; // 选中值
var q=parseInt(text1.value);
var h=parseInt(text2.value);
switch(aa){
case '+':
text3.value=q+h;
break;
case '-':
text3.value=q-h;
break;
case '*':
text3.value=q*h;
break;
case '/':
text3.value=q/h;
break;
}
}
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select' value="a">
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick='a()'/> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
</body>
</html>
代码来看下