Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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)有什么区别?什么时候应该使用哪个?这个返回一个本地JavaScript对象(如果我说得对的话),$(这个)返回一个jQuery对象 $(this)[0] == this 如果您考虑上面的函数,jQuery将循环遍历页面上的每个段落元素,并通过将“这个”变量传递给匿名函数来返回对每个段落元素的引用。如果“this”变量包装在jQuery函数($(this))中,那么我们可以访问与元素相关的所有jQuery属性,例如$(this).find('span')。

jQuery中的this和$(this)有什么区别?什么时候应该使用哪个?

这个
返回一个本地JavaScript对象(如果我说得对的话),
$(这个)
返回一个jQuery对象

$(this)[0] == this

如果您考虑上面的函数,jQuery将循环遍历页面上的每个段落元素,并通过将“这个”变量传递给匿名函数来返回对每个段落元素的引用。如果“this”变量包装在jQuery函数($(this))中,那么我们可以访问与元素相关的所有jQuery属性,例如

$(this).find('span')
。“this”对象本身只是一个普通的Javscript DOM对象

$()是jQuery构造函数

这是对调用的DOM元素的引用

因此,基本上,在$(this)中,您只是将this作为参数传递到$()中,以便可以调用jQuery方法和函数

重复:

可能重复的
$('p').each(function () {
  //this.id;
  //$(this).attr('id');
})