javascript - jQuery 中的 this 和 $(this) 有什么区别?

标签 javascript jquery

jQuery 中 this 和 $(this) 有什么区别?我什么时候应该使用哪个?

最佳答案

$('p').each(function () {
  //this.id;
  //$(this).attr('id');
})

如果您考虑上面的函数,jQuery 将遍历页面上的每个段落元素,并通过将“this”变量传递给匿名函数来返回对每个段落元素的引用。如果 'this' 变量被包装在 jQuery 函数 ($(this)) 中,那么我们可以访问与元素相关的所有 jQuery 优点,例如 $(this).find('span') . “this”对象本身就是一个普通的 Javscript DOM 对象。

关于javascript - jQuery 中的 this 和 $(this) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965979/

相关文章:

jquery - 不丢失 jquery 上的焦点事件

javascript - 如何将另一个文件中的实用程序函数包含到 Sproutcore 单元测试文件中?

javascript - 根据数字检查字符串是一种好的风格吗?

javascript - 粘性导航 - 无法读取未定义的属性 'top'”

jquery - jquery 问题和更新 css 背景图像未在 chrome 中加载

JavaScript - 取决于下拉值

javascript - 无法旋转文本d3js

javascript - Js函数只工作一次

java - JavaScript 中的 JSP 标签警报

javascript - 创建复杂的表结构