hivefans

javascript闭包

Aug 22nd, 2013
143
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>闭包演示</title>
  6. function init() {    
  7.     var pAry = document.getElementsByTagName("p");    
  8.     for( var i=0; i<pAry.length; i++ ) {
  9.            
  10.             pAry[i].onclick=Function("alert("+i+")");
  11.        
  12.   }
  13. }
  14. </head>
  15. <body onload="init();">
  16. <p>产品 0</p>
  17. <p>产品 1</p>
  18. <p>产品 2</p>
  19. <p>产品 3</p>
  20. <p>产品 4</p>
  21. </body>
  22. </html>
RAW Paste Data