js/jquery this 坑

重要:js onclick() 函数中,取不到this !!!

错误的写法:

function test(){
$(this).parent().addClass(‘active‘)
}

正确的写法是:函数定义时带参数,调用时传入this

function test(param){
$(param).parent().addClass(‘active‘)
}

<button onclick=”test(this)”>