日期:2014-05-16 浏览次数:20327 次
var sMessage = "Hello World"; function sayHelloWorld(){ alert(sMessage); }; sayHelloWorld();
<ul> <li id="a1">aa</li> <li id="a2">aa</li> <li id="a3">aa</li> </ul> <script type="text/javascript"> <ul> <li id="a1">aa</li> <li id="a2">aa</li> <li id="a3">aa</li> </ul> <script type="text/javascript"> for(var i=1; i < 4; i++){ var id = document.getElementById("a" + i); id.onclick = function(){ alert(i);//现在都是返回4 } } </script>
for(var i=1; i < 4; i++){ var id = document.getElementById("a" + i); id.onclick = new function(){ var i2=i;//这个i是闭包的闭包 return function(){ alert(i2); } }; }