索引值的应用

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script>
            window.onload = function(){
                var aBtn = document.getElementsByTagName('input');
                var aP = document.getElementsByTagName('p');
                var arr=['kk','li','ll'];
                for(var i = 0;i<aBtn.length;i++){
                    aBtn[i].index= i;
                    aBtn[i].onclick = function(){
                        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"/>
        <p>5</p>
        <p>7</p>
        <p>9</p>
    </body>
</html>
示例代码
posted @ 2017-05-26 20:10  美琳  阅读(116)  评论(0编辑  收藏  举报