JavaScript 学习笔记— —自定义属性

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload=function()
{
    //自定义属性
    var oDiv1=document.getElementById("div1");
    var aBtn=oDiv1.getElementsByTagName("input");
    var arr=['A','B','C','D']
    for(var i=0;i<aBtn.length;i++){
        aBtn[i].num=0;//给input标签增加num="0"属性
        aBtn[i].onclick=function(){
            this.value=arr[this.num];
            this.num++;
            if(this.num==arr.length){
                this.num=0;
            }
        }
    }

    //自定义索引
    var oDiv2=document.getElementById("div2");
    var oBtn=oDiv2.getElementsByTagName("input");
    var arrCity=['北京','上海','合肥']
    for(var i=0;i<oBtn.length;i++){
        oBtn[i].index=i;//自定义索引
        oBtn[i].onclick=function(){
            this.value=arrCity[this.index];
        }
    }
}
</script>
</head>
<body>
<div id="div1">
    <input type="button" value="0" />
    <input type="button" value="0" />
    <input type="button" value="0" />
</div>

<div id="div2">
    <input type="button" value="0" />
    <input type="button" value="0" />
    <input type="button" value="0" />
</div>

</body>
</html>

 

posted @ 2015-04-02 15:41  eaysun  阅读(137)  评论(0编辑  收藏  举报