自定义属性

来源:互联网 发布:已备案过期删除域名 编辑:程序博客网 时间:2024/05/21 12:50


[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>自定义属性</title>  
  6. <script>  
  7. window.onload = function(){  
  8.     var aBtn = document.getElementsByTagName('input');  
  9.       
  10.     for(var i=0; i<aBtn.length; i++){  
  11.         aBtn[i].index = i;  
  12.         aBtn[i].onclick = function(){  
  13.             alert('我是第' + this.index + '个按钮');  
  14.         };    
  15.     }     
  16. };  
  17. </script>  
  18. </head>  
  19.   
  20. <body>  
  21. <input type="button" value="按钮">  
  22. <input type="button" value="按钮">  
  23. <input type="button" value="按钮">  
  24. </body>  
  25. </html>  

思路:

这里的自定义属性,其实就是给每个按钮添加一个索引值,然后通过索引值找到对应的按钮

0 0
原创粉丝点击