I am currently working through this tutorial: [Getting Started with jQuery][1]
[1]: http://docs.jquery.com/Tutorials:Getting_Started_with_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. In the second example we use `this` directly when resetting the form.
`$(this)` seems to be used a lot more often than `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.
Basically we need `$()` for special jQuery-only functions.
Is this correct? @Reigel, why was this protected? The OP questioned and guessed the correct answer.
以上就是What's the difference between '$(this)' and 'this'?的详细内容,更多请关注web前端其它相关文章!