索引值应用

微笑、不失礼 提交于 2020-01-14 19:38:44

 

<script> 

window.onload = function (){ 

    var aBtn = document.getElementsByTagName('input'); 

    var aP = document.getElementsByTagName('p'); 

     

    // 想建立“匹配”“对应”关系,就用索引值 

    var arr = [ '莫涛', '张森', '杜鹏' ]; 

     

    for( var i=0; i<aBtn.length; i++ ){ 

         

        aBtn[i].index = i; // 自定义属性(索引值) 

         

        aBtn[i].onclick = function (){ 

            // alert( arr[ this.index ] ); 

            this.value = arr[ this.index ]; 

             

            aP[ this.index ].innerHTML = arr[ this.index ]; 

        }; 

    } 

}; 

</script> 

</head> 

 

<body> 

 

<input type="button" value="btn1" /> 

<input type="button" value="btn2" /> 

<input type="button" value="btn3" /> 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!