jquery 绑定事件 this
jQuery 事件绑定中的 this 用法
在 jQuery 中,this 在事件处理函数中通常指向触发事件的 DOM 元素。以下是几种常见的使用场景和注意事项:
直接事件绑定中的 this
通过 .on() 或 .click() 等方法绑定事件时,this 指向当前触发事件的 DOM 元素:

$("#button").on("click", function() {
// this 指向被点击的 button 元素
console.log(this); // 输出 DOM 元素
$(this).text("Clicked"); // 使用 jQuery 包装 this
});
箭头函数中的 this
箭头函数会继承外层作用域的 this,因此在事件处理中可能导致意外行为:

$("#button").on("click", () => {
// 箭头函数中的 this 不会指向 DOM 元素
console.log(this); // 可能指向 window 或其他上下文
});
动态生成的元素事件绑定
对于动态生成的元素,需通过事件委托绑定事件。此时 this 仍指向触发事件的子元素:
$(document).on("click", ".dynamic-button", function() {
console.log(this); // 指向 .dynamic-button 元素
});
显式指定上下文
通过 jQuery 的 .proxy() 或 ES6 的 .bind() 可以手动设置 this:
$("#button").on("click", function() {
console.log(this); // 强制绑定到特定对象
}.bind(customObject));
注意事项
- 使用
$(this)将 DOM 元素转换为 jQuery 对象以调用 jQuery 方法。 - 避免在事件处理函数中混用箭头函数和普通函数。
- 事件委托时需确保选择器匹配正确的目标元素。






