JavaScript之自定义属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload = function(){
var aBtn = document.getElementsByTagName(input);
for (var i = 0; i < 3; i ++){
aBtn[i].abc = 123;
}
}
</script>
</head>
<body>
<input type="button" value="按钮1">
<input type="button" value="按钮2">
<input type="button" value="按钮3">
</body>
</html>
for (var i = 0; i < 3; i ++){
aBtn[i].abc = 123;
}
在每一个按钮后面添加了一个自定义的属性,可以进行读写操作;
自定义的应用1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload = function(){
var aBtn = document.getElementsByTagName('input');
var arr = ['A','B','C','D'];
for (var i =0; i < aBtn.length; i++) {
aBtn[i].num = 0;
aBtn[i].onclick = function(){
this.value = arr[this.num];
this.num ++;
if (this.num == arr.length) {
this.num = 0;
};
}
}
}
</script>
</head>
<body>
<input type="button" value="按钮">
<input type="button" value="按钮">
<input type="button" value="按钮">
</body>
</html>
可以单个控制每个按钮,每点击一次更换按钮内的内容