Javascript 一共得到jquery的.each()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3347997/
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
get a total of jquery's .each()
提问by Mike Rifgin
I'm using jquery's .each() to iterate over a group of li's. I need a total of all the li's matched. Is the only way to create a count variable outside the .each() and increment this inside the .each()? It doesn't seem very elegant.
我正在使用 jquery 的 .each() 来迭代一组 li。我需要所有 li 的匹配项。是在 .each() 之外创建计数变量并在 .each() 内部增加它的唯一方法吗?看起来不是很优雅。
var count;
$('#accordion li').each(function() {
++count;
});
回答by Owen
Two options:
两种选择:
$('#accordion li').size(); // the jQuery way
$('#accordion li').length; // the Javascript way, which jQuery uses
Since jQuery calls length under the hood, it's faster to use that instead of the size() call.
由于 jQuery 在幕后调用 length,因此使用它代替 size() 调用会更快。
回答by Colin Brock
$('#accordion li').length;
回答by user113716
Well, I just saw this question, and you already accepted an answer, but I'm going to leave one anyway.
嗯,我刚看到这个问题,你已经接受了一个答案,但我还是要留下一个。
The point of the question seems to be concerned with incrementing a counter.
问题的重点似乎与增加计数器有关。
The fact is that jQuery's .each()method takes care of this for you. The first parameter for .each()isan incrementing counter, so you don't need to do it yourself.
事实上,jQuery 的.each()方法会为您处理这些问题。for的第一个参数.each()是一个递增计数器,所以你不需要自己做。
$('#accordian li').each(function(index) {
// index has the count of the current iteration
console.log( index );
});
So as you can see, there is an elegant solution built in for you.
如您所见,为您内置了一个优雅的解决方案。
回答by Arron
Ok so the best way to do this is as follows: firstly map the wrapped set to a variable so you never have to do the sizzle dom lookup again:
好的,最好的方法如下:首先将包装的集合映射到一个变量,这样你就不必再次进行 sizzle dom 查找:
var $myListItems = $('#accordian li');
Note: my preference is to put $ at the beginning of any vars that are a jQuery wrapped set, hence $myListItems as opposed to myListItems
注意:我的偏好是将 $ 放在任何 jQuery 包装集的变量的开头,因此 $myListItems 而不是 myListItems
Then set a var outside the each function that has the length of the wrapped set:
然后在具有包装集长度的 each 函数之外设置一个 var:
var myLength = $myListItems.length;
Note the lack of a $ here as the var is not a jQuery object.
注意这里没有 $,因为 var 不是 jQuery 对象。
Now run the each function with a variable for the index.
现在使用索引变量运行 each 函数。
$myListItems.each(function(index){
// myLength has the length
// index has the current, 0 based index.
});
You now have what you've asked for in the OP with only one look up and so need to fumble in the each function with having to know the contents of the wrapped set just to know the length of it. the beauty of this over a counter is that on every iteration of the each you already know the length.
您现在只需查找一次即可在 OP 中获得所需的内容,因此需要在每个函数中摸索,而必须知道包装集的内容才能知道它的长度。柜台交易的美妙之处在于,在每个迭代的每次迭代中,您都已经知道长度。
回答by Brad Hobson
One of the big issues, which I have not found a fix for yet, is if you have two sets of the same thing you will iterate through.
我还没有找到解决方案的一个大问题是,如果你有两组相同的东西,你将迭代。
For instance, 2 table heads that you are iterating through number of column headers. If you use length on ( $item.children().find('thead tr th').each...) for your each clause and you have 2 tables it will double the amount of your length that you are trying to walk through potentially [5 column headers in each table will return length of 10].
例如,您正在迭代多个列标题的 2 个表头。如果您对每个子句使用 length on ( $item.children().find('thead tr th').each...) 并且您有 2 个表,它将使您尝试行走的长度增加一倍通过可能 [每个表中的 5 个列标题将返回长度为 10]。
You could do both on a id name and have those different but if adding dynamically then this can become a headache.
您可以在一个 id 名称上同时进行,并使用不同的名称,但如果动态添加,则这可能会令人头疼。
The only way I found once inside the foreach is to use $(this):
我在 foreach 中找到的唯一方法是使用 $(this):
$item.children().find('thead tr th').each(function(i, th) {
// I am working with a table but div could be used, or something else.
// I have 2 tables with 5 column headers, the below len return
var len = $(this).closest('table').find('thead tr th').length;
if (i < len) {
// ...your code here
}
});

