自定义属性,匹配数组内容——JS学习笔记2015-5-26(第39天)

使用自定义属性匹配数组内容

<!DOCTYPE HTML>
<html>
<head>
<title>匹配数组内容</title>
<meta charset="utf-8">
<script>
    window.onload = function(){
      var aBtn=document.getElementsByTagName('input');
      var arr = ['A','B','C','D'];
      
      for(var i=0; i<aBtn.length; i++){ // 这里的for循环,主要是为了找到页面中所有的按钮,然后让每个按钮实现for循环内的功能;
            aBtn[i].num = 0;
            aBtn[i].onclick = function(){
              this.value = arr[this.num]; // 不是arr[i] 理解
              this.num++; // 这里的num自增是发生在按钮点击这个事件之下发生的
              if(this.num===arr.length){
                   this.num = 0;
              }
            }
       }
        
  }
  
</script>
</head>

<body>
<input type="button" value="0"><input type="button" value="0"><input type="button" value="0">
</body>
</html>
原文地址:https://www.cnblogs.com/zhangxg/p/4532137.html