多个按钮,每个按钮每点击一次文字描述根据数组依次变换
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; } }; }
<input type="button" value="0" /> <input type="button" value="0" /> <input type="button" value="0" />