Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - '$(this)'和'this'有什么区别?(What's the difference between '$(this)' and 'this'?)

I am currently working through this tutorial: Getting Started with jQuery(我目前正在研究本教程: jQuery入门)

For the two examples below:(对于以下两个示例:) $("#orderedlist").find("li").each(function (i) { $(this).append(" BAM! " + i); }); $("#reset").click(function () { $("form").each(function () { this.reset(); }); }); Notice in the first example, we use $(this) to append some text inside of each li element.(注意在第一个示例中,我们使用$(this)在每个li元素内附加一些文本。) In the second example we use this directly when resetting the form.(在第二个示例中,我们在重置表单时直接使用this 。) $(this) seems to be used a lot more often than this .($(this)似乎比this经常使用。) My guess is in the first example, $() is converting each li element into a jQuery object which understands the append() function whereas in the second example reset() can be called directly on the form.(我的猜测是在第一个示例中, $()将每个li元素转换为一个能够理解append()函数的jQuery对象,而在第二个示例中, reset()可以直接在表单上调用。) Basically we need $() for special jQuery-only functions.(基本上,我们需要$()用于特殊的仅jQuery函数。) Is this correct?(这个对吗?)   ask by Kevin Wu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes you only need $() when you're using jQuery.(是的,使用jQuery时只需要$() 。)

If you want jQuery's help to do DOM things just keep this in mind.(如果您想要jQuery的帮助来做DOM事情,请记住这一点。) $(this)[0] === this Basically every time you get a set of elements back jQuery turns it into a jQuery object .(基本上,每次您返回一组元素时,jQuery都会将其转换为jQuery对象 。) If you know you only have one result, it's going to be in the first element.(如果您知道只有一个结果,它将在第一个元素中。) $("#myDiv")[0] === document.getElementById("myDiv"); And so on...(等等...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...