js 如何在多个按钮中获取点击按钮的id值

发布时间:2021-06-18 09:49:29    发布者:文昌文城莱奥网络技术工作室    浏览次数:992

window.onload = function(){
var arr = document.getElementsByTagName('button');
for(var i = 0;i<arr.length;i++){
arr[i].onclick = function(){
alert(this.id);
}
}
}

<button id="b1">b1</button>
<button id="b2">b2</button>
<button id="b3">b3</button>
<button id="b4">b4</button>