这和jQuery中的$(this)有什么区别? [英] What is the difference between this and $(this) in jQuery?
问题描述
这和jQuery中的$(this)有什么区别?我什么时候应该使用哪个?
What is the difference between this and $(this) in jQuery? And when should I use which?
推荐答案
$('p').each(function () {
//this.id;
//$(this).attr('id');
})
如果你考虑上面的函数,jQuery将遍历页面上的每个段落元素,并通过将'this'变量传递给每个段落元素来返回对它的引用匿名函数。如果'this'变量包含在jQuery函数($(this))中,那么我们可以访问与元素相关的所有jQuery优点,例如 $(this).find('span')
。它本身的'this'对象只是一个普通的Javscript DOM对象。
If you consider the function above jQuery will loop through each paragraph element on the page and will return a reference to each paragraph element by passing the 'this' variable into the anonymous function. If the 'this' variable is wrapped in the jQuery function ($(this)) then we can access all the jQuery goodness in relation to the element e.g $(this).find('span')
. The 'this' object on it's own is just a normal Javscript DOM Object.
这篇关于这和jQuery中的$(this)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!