js/jquery this 坑

Posted 寻坑者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js/jquery this 坑相关的知识,希望对你有一定的参考价值。

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

错误的写法:

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

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

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

<button onclick="test(this)">

 

以上是关于js/jquery this 坑的主要内容,如果未能解决你的问题,请参考以下文章