javascript - jQuery this 内部事件

标签 javascript jquery dom

我以为我真的理解 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/

相关文章:

javascript - 由于 CSS,Codemirror 显示不正确

javascript - JS - 用 html 解析 JSON

javascript - 防止浏览器在没有哈希值的情况下刷新时自动滚动

jquery - 如何使用显式变量而不是列名称

java - 如何将dom4j文档对象转换为字符串

javascript - Ajax 从 mysql 查询填充表单

javascript - 在请求期间创建可用的 URL

jquery - 限制ajax请求的方法

javascript ElementById 说明

javascript - 如何通过 id 注册删除 dojo 小部件,但未在任何 DOM 节点中引用