Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:$(this)和this之间的差异_Javascript_Jquery - Fatal编程技术网

Javascript jQuery:$(this)和this之间的差异

Javascript jQuery:$(this)和this之间的差异,javascript,jquery,Javascript,Jquery,我试图了解jQuery中$(this)和this之间的区别,并最终找到从this获取$(this)对象的方法(thiswhat??): 如中所述,我需要使用this而不是$(this)对象,因为这是将jQuery对象分配给var而不丢失其实例的唯一方法 我该怎么做呢?在您显示的代码中,这是对纯DOM对象的引用,该对象的ID为被单击的元素 $(此)将DOM对象扩展为一个,这使得所有jQuery类addClass都可用于它。在您显示的代码中,此是对被单击的ID元素的纯DOM对象的引用 $(这)将DO

我试图了解
jQuery
$(this)
this
之间的区别,并最终找到从
this
获取
$(this)
对象的方法(
this
what??):

如中所述,我需要使用
this
而不是
$(this)
对象,因为这是将
jQuery
对象分配给var而不丢失其实例的唯一方法


我该怎么做呢?

在您显示的代码中,
这是对纯DOM对象的引用,该对象的ID为被单击的
元素


$(此)
将DOM对象扩展为一个,这使得所有jQuery类
addClass
都可用于它。

在您显示的代码中,
是对被单击的ID
元素的纯DOM对象的引用


$(这)
将DOM对象扩展为一个类,这使得所有jQuery类的优点都可用于它。

@Pekka是正确的,我发现这篇文章很重要,可以通过以下方式在这里发表:


@Pekka是对的,我觉得这篇文章很重要,可以通过以下方式在这里发表:


谢谢您的解释,所以我不能从DOM对象中获取jQuery对象吗?@Vittorio no,没有
$()
。DOM对象本身并不包含jQuery对象。对于jQuery,您必须首先使用
$()
基于DOM对象创建对象。感谢您的解释,所以我不能从DOM对象获取jQuery对象吗?@Vittorio no,没有
$()
。DOM对象本身并不包含jQuery对象。您必须使用jQuery的
$()
首先基于DOM对象创建对象。
var last_btn;
$("#element").click (function () {
    if (last_btn != null && last_btn == this) {
        // to unselect the current button
        last_btn.removeClass ("selected"); // doesn't work because this is not $(this)
    } else {
        if (last_btn != null) last_btn.removeClass ("selected"); // to unselect another old button
        last_btn = this;
        $(this).addClass ("selected");
    }
});