标签:style blog http java color os
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> /** * 此时执行始终提示 link#3 * 因为页面加载完成以后 循环已经执行完成 i=3 */ /* function MyDemo(){ var elems = document.getElementsByTagName(‘a‘); //alert(elems.length) //elems[0].attachEvent //alert(typeof elems[0].addEventListener) ; //firefox //alert(typeof elems[0].attachEvent) ; //ie for(var i = 0,max = elems.length ;i<max ;i++){ elems[i].addEventListener("click",function(e){ e.preventDefault() ; alert("link #" +i) ; },false) ; } } */ function MyDemo(){ var elems = document.getElementsByTagName(‘a‘); for(var i = 0,max = elems.length ;i<max ;i++){ (function(i){ elems[i].addEventListener("click",function(e){ e.preventDefault() ; alert("link #" +i) ; },false) ; })(i) ; } } var counter = (function(){ var i = 0 ; return { get: function(){ return i ; }, set:function(val){ i = val ; }, increment:function(){ i++ ; } } }()) ; alert(counter.get()) ; counter.set(20) ; alert(counter.get()) ; counter.increment() ; alert(counter.get()) ; </script> </head> <body onload="MyDemo();"> <a href="#" >Hello01</a><br/> <a href="#" >Hello02</a><br/> <a href="#" >Hello04</a> </body> </html>
JAVASCRIPT基础02-自执行,布布扣,bubuko.com
标签:style blog http java color os
原文地址:http://www.cnblogs.com/liaokailin/p/3822792.html