标签:onclick class color dom nbsp code 机制 pre click
HTML代码:
<button type=‘button‘ class=‘btn‘ id=‘1‘>按钮1</button> <button type=‘button‘ class=‘btn‘ id=‘2‘>按钮1</button> <button type=‘button‘ class=‘btn‘ id=‘3‘>按钮1</button> <button type=‘button‘ class=‘btn‘ id=‘4‘>按钮1</button>
JS代码:
var btn = document.querySelectorAll(‘.btn‘); for(var i =0; i<btn.length; i++) { ((i)=>{ btn[i].onclick = ()=>{ aleter(‘我是按钮‘+btn[i].getAttribute(‘id‘)); }; })(i); }
标签:onclick class color dom nbsp code 机制 pre click
原文地址:https://www.cnblogs.com/Liqian-Front-End-Engineer/p/11910712.html