2017-10-04 33 views
0

为什么addEventListenerfunction()在注释的代码中一起使用,但没有通过特定的功能?为什么addEventListener与函数()一起工作,但没有使用我传递的特定函数?

var button = document.querySelector("button"); 

// button.addEventListener("click", function(){ 
// button.classList.toggle("bigyellow"); 
// }); 

button.addEventListener("click", toggleMe); 
function toggleMe(){  
    this.classList.toggle("big-yellow"); 
} 


//CSS: 
//.bigyellow { 

// background-color: #ffff66; /* yellow */ 
//} 
+0

工作正常,我....但是,你就引用它,而不是依赖于吊装前,真的应该声明你的函数。 – epascarello

+0

@Cruiser - 'this'里面'toggleMe' *应该*是点击的按钮 –

+1

哦亲爱的......'toggle(“bigyellow”);'!=='toggle(“big-yellow”);'' –

回答

相关问题