使用自定义属性匹配数组内容
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>匹配数组内容</title> 5 <meta charset="utf-8"> 6 <script> 7 window.onload = function(){ 8 var aBtn=document.getElementsByTagName(‘input‘); 9 var arr = [‘A‘,‘B‘,‘C‘,‘D‘]; 10 11 for(var i=0; i<aBtn.length; i++){ 12 aBtn[i].num = 0; 13 14 aBtn[i].onclick = function(){ 15 this.value = arr[this.num]; // 不是arr[i] 理解 16 this.num++; 17 if(this.num===arr.length){ 18 this.num = 0; 19 } 20 } 21 } 22 23 } 24 25 </script> 26 </head> 27 28 <body> 29 <input type="button" value="0"><input type="button" value="0"><input type="button" value="0"> 30 </body> 31 </html>
时间: 2024-12-22 16:01:41