在元素之前计数元素 [英] Count elements before an element
本文介绍了在元素之前计数元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个示例:
<div class="create-q"></div>
<div class="create-q"></div>
<div class="create-q"></div>
<div class="create-q"></div> <-- im in this class : I want to know how many div with class "create-q" there is before this one. (3 in this example)
<div class="create-q"></div>
<div class="create-q"></div>
我知道如何计算,但不知道如何达到指定的格数.
I know how to count but not how to stop it once it reach a specified div..
我该如何使用jQuery?
How can I do this with jQuery ?
推荐答案
如果mydiv
是对您已经在查看的元素的引用:
If mydiv
is a reference to the element you're already looking at:
var n = $(mydiv).index('.create-q');
将给出该div的索引,因为它们是从零开始的,也恰好是前一个div的数量.
will give the index of that div which as they're zero-based also happens to be the number of preceding divs.
与.prevAll()
不同,无论div是否具有公共父级,这都将起作用.
Unlike .prevAll()
this will work regardless of whether the divs have a common parent or not.
这篇关于在元素之前计数元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文