我以为我真的理解 jQuery/JS 中 this
的使用,但这表明我错了。我想在 DOM 元素上添加类,我使用了这个:
$("#content").on("click",function(){
this.addClass("bonus");
});
但是什么也没发生,所以我将第二行代码更改为并且它起作用了:
$("#content").addClass("bonus");
第一个示例中的 this
不应该引用 $("#content")
吗?
最佳答案
this
本身就是原生 DOM 元素,您需要 $(this)
这是一个 jQuery 元素,其方法为 addClass
关于javascript - jQuery this 内部事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26918317/