一個函數(shù),當(dāng)事件發(fā)生的時候觸發(fā)事件的元素改變屬性,要用到this參數(shù)。用addEventListener不能傳參數(shù)(我想傳一個this過去),網(wǎng)上的方法有點(diǎn)復(fù)雜看不懂,所以我想到用setAttribute的方法試試,沒想到也能運(yùn)行,但是網(wǎng)上說這方法不行,為什么;var a = true;function fn(_this){if(a){_this.style.background='#f00';}else{_this.removeAttribute("style");}a = !a;}document.getElementById('input1').setAttribute('onclick','fn(this)');//HTML<form action="456" method="get" accept-charset="utf-8"><input id="input1" type="button" name="a123" value="645" style="color:#ccc"></form>
小白問個事件監(jiān)聽傳參數(shù)的問題
路人戊_
2018-02-25 23:41:20