javascript 每个函数索引和元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5865240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:37:06  来源:igfitidea点击:

each function index and element

javascriptjquery

提问by ONYX

my question is a simple one how do you use element with index in each function

我的问题很简单,你如何在每个函数中使用带索引的元素

$('div').each(function(index, element) {
     is element equal to $(this)
});

采纳答案by alex

The elementthere will alwaysbe the same as this.

element那里将永远是相同的this

jsFiddle.

js小提琴

Except wrapping it in $()will make it a jQuery object, and won't be equal to the other one, even if you wrap the other with a jQuery object.

除了将它包裹起来$()会使其成为一个 jQuery 对象,并且不会等于另一个,即使你用一个 jQuery 对象包裹另一个。

There should never be a reason why you need to compare thisto elementin that context.

永远不应该有,为什么你需要比较的理由this,以element在这方面。

回答by JohnP

$('div').each(function(index, element) {
     //element != $(this)
     //element == this
});

$(this)is thiswrapped by a jquery object. So while thiswon't equal $(this), you can still manipulate it to your heart's content

$(this)this由一个jquery对象缠绕。因此,虽然this不会等于$(this),但您仍然可以随心所欲地操纵它

Here's something to look at : http://jsfiddle.net/jomanlk/ZqXPn/

这里有一些东西要看:http: //jsfiddle.net/jomanlk/ZqXPn/