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
each function index and element
提问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 element
there will alwaysbe the same as this
.
在element
那里将永远是相同的this
。
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 this
to element
in that context.
永远不应该有,为什么你需要比较的理由this
,以element
在这方面。
回答by JohnP
$('div').each(function(index, element) {
//element != $(this)
//element == this
});
$(this)
is this
wrapped by a jquery object. So while this
won'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/