码迷,mamicode.com
首页 > 其他好文 > 详细

关于 document.getElementById 一个奇怪的现象

时间:2016-08-13 22:28:23      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

html:

<html>
<head>
<script type="text/javascript" src="1.js"></script>
</head>
<body>
<input type="text" id="num1"/><br/>
<input type="text" id="num2"/><br/>
<input type="button" onclick="func1()" value=" + "/>
<input type="button" onclick="func1()" value=" - "/>
<input type="button" onclick="func1()" value=" * "/>
<input type="button" onclick="func1()" value=" / "/>
</body>
</html>

js:

function func1(){
var a=document.getElementById(‘num1‘).value;
alert(a);
}

可以正确的读取num1的内容,但是将js改为:

var a=document.getElementById(‘num1‘).value;

function func1(){

alert(a);
}

 就不能了,很奇怪,初学者搞不懂,求大神指点。

关于 document.getElementById 一个奇怪的现象

标签:

原文地址:http://www.cnblogs.com/yuanceo/p/5768988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!