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/