1.源代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>尝试的path</title> <meta name="description" content=""> <meta name="keywords" content=""> <link href="" rel="stylesheet"> </head> <body> <!--自定义属性应用--> <!--主要是点击按钮,数字增加0变成A,B,C,D或者其它类似--> <input type="button" value="0"> <input type="button" value="0"> <input type="button" value="0"> <script type="text/javascript"> window.onload = function(){ var aBtn = document.getElementsByTagName('input'); var arr = ['A','B','C','D']; //定义数组,当点击按钮时按钮中的值由0变成A,B,C,D for(var i=0;i<aBtn.length;i++){ aBtn[i].num = 0; //为button自定义属性,从0开始 aBtn[i].onclick = function(){ this.value = arr[this.num]; //arr[this.num]表示的是arr[0]——从自定义属性num可以看出。其中this表示的是这个按钮而不是window,所以this.value表示的是这个按钮中的value值,=表示赋值 //alert(arr[this.num]);//点击一开始就是0 this.num++; if(this.num==arr.length){//运用if的原因是当点击按钮出现数字D后,会跑到数组外,所以要用if进行条件判断 this.num=0; } } } } </script> </body> </html>随机点击一个按钮,按钮值随之改变:
