一個函數,當事件發生的時候觸發事件的元素改變屬性,要用到this參數。用addEventListener不能傳參數(我想傳一個this過去),網上的方法有點復雜看不懂,所以我想到用setAttribute的方法試試,沒想到也能運行,但是網上說這方法不行,為什么;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>
小白問個事件監聽傳參數的問題
路人戊_
2018-02-25 23:41:20