'$(this)'和'this'之间有什么区别? [英] What's the difference between '$(this)' and 'this'?

查看:207
本文介绍了'$(this)'和'this'之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在完成本教程: jQuery入门

I am currently working through this tutorial: Getting Started with jQuery

以下两个例子:

$("#orderedlist").find("li").each(function (i) {
    $(this).append(" BAM! " + i);
});
$("#reset").click(function () {
    $("form").each(function () {
        this.reset();
    });
});

注意在第一个例子中,我们使用 $(this)在每个 li 元素中附加一些文本。在第二个示例中,我们在重置表单时直接使用

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 )似乎经常使用这个

我的猜测是在第一个例子中, $()将每个 li 元素转换为一个理解<$ c $的jQuery对象c> append()函数,而在第二个示例中, reset()可以直接在表单上调用。

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.

基本上我们需要 $()来获取特殊的jQuery函数。

Basically we need $() for special jQuery-only functions.

这是正确吗?

推荐答案

是的,你需要 $()当你'重新使用jQuery。如果你想让jQuery帮助做DOM事情,请记住这一点。

Yes you only need $() when you're using jQuery. If you want jQuery's help to do DOM things just keep this in mind.

$(this)[0] === this

基本上每当你得到一组元素时,jQuery会把它变成 jQuery对象。如果你知道你只有一个结果,它就会出现在第一个元素中。

Basically every time you get a set of elements back jQuery turns it into a jQuery object. 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...

这篇关于'$(this)'和'this'之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆