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

发布时间:2021-10-08 09:07:56    发布者:文昌文城莱奥网络技术工作室    浏览次数:927

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>


https://blog.csdn.net/u010965170/article/details/78748107