首页 > 其他 > 详细

索引值应用

时间:2020-01-14 19:34:21      阅读:73      评论:0      收藏:0      [点我收藏+]

<script> 

window.onload = function (){ 

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

     

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

    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 ]; 

        }; 

    } 

}; 

</script> 

</head> 

 

<body> 

 

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

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

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

 

</body> 

 

首先获取相关元素,然后通过for循环出所有的按钮,然后给每个按钮添加一个自定义索引值,当点击按钮时,可以alert一下当前被点击按钮的索引值是多少(测试),

 

接着当点击按钮是,通过数组的索引值赋值给当前点击的按钮的value上。

索引值应用

原文:https://www.cnblogs.com/tongguilin/p/12193583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!