Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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和#x27;s$(选择器)。每个()和#x27;元素';参数,vs$(此)_Javascript_Jquery - Fatal编程技术网

Javascript 获取jQuery和#x27;s$(选择器)。每个()和#x27;元素';参数,vs$(此)

Javascript 获取jQuery和#x27;s$(选择器)。每个()和#x27;元素';参数,vs$(此),javascript,jquery,Javascript,Jquery,在获取jQuery.each()的元素参数时是否有任何差异(与性能或其他方面相关) $(".element").each(function(i, e){ console.log("element: " + $(e)); }); 使用$(this) 我做了几次测试,在程序上我没有发现任何差异。我总是使用$(this),因为它是大多数应用程序使用的标准。不,没有实际区别。在中,我们可以看到相同的东西(obj[i])被传递到call,用作此和回调中的第二个参数: value = callbac

在获取jQuery
.each()
元素
参数时是否有任何差异(与性能或其他方面相关)

$(".element").each(function(i, e){
   console.log("element: " + $(e));
});
使用
$(this)


我做了几次测试,在程序上我没有发现任何差异。我总是使用
$(this)
,因为它是大多数应用程序使用的标准。

不,没有实际区别。在中,我们可以看到相同的东西(
obj[i]
)被传递到
call
,用作
和回调中的第二个参数:

value = callback.call(obj[i], i, obj[i]);

不,没有实际的区别。在中,我们可以看到相同的东西(
obj[i]
)被传递到
call
,用作
和回调中的第二个参数:

value = callback.call(obj[i], i, obj[i]);

虽然在您的示例中没有实际的区别,但是在一个封闭函数或方法调用中
e
的可用性是一个有用的特性

例如:

$('.element').each(function(i, e) {
    $('.otherElement').text(function(index, element){

      // in this method, $(this) is an element from
      // the $('.otherElement) collection (also $(element));
      // to access elements of the $(`.element') collection
      // we can no longer use $(this), but we can still use
      // $(e) (or e):

      return $(e).eq(index).text(); // will set the text of the $(element)
                          // to the text of the $(e) element
    });
})

虽然在您的示例中没有实际的区别,但是在一个封闭函数或方法调用中
e
的可用性是一个有用的特性

例如:

$('.element').each(function(i, e) {
    $('.otherElement').text(function(index, element){

      // in this method, $(this) is an element from
      // the $('.otherElement) collection (also $(element));
      // to access elements of the $(`.element') collection
      // we can no longer use $(this), but we can still use
      // $(e) (or e):

      return $(e).eq(index).text(); // will set the text of the $(element)
                          // to the text of the $(e) element
    });
})

不,没有区别,
这个
是一个特殊的变量,在本例中它缓存元素,
el
是一个正则变量,它缓存元素,所以它实际上是完全一样的。不,没有区别,
这个
是一个特殊的变量,在本例中它缓存元素,而
el
是一个缓存元素的正则变量,所以它实际上是完全相同的。