【发布时间】:2014-09-12 03:43:41
【问题描述】:
我正在尝试根据子对象的相似类而不是 tag 名称来计算对象的 index 值。
认为type_a 和type_b 元素之间有一个分隔容器。我写了 index 值,我想得到,但找不到解决这个问题的方法。
html:
<div class="container">
<span class="type_a">0</span>
<span class="type_a">1</span>
<span class="type_b">0</span>
<span class="type_a">2</span>
<span class="type_b">1</span>
<span class="type_b active">2</span>
<span class="type_a">3</span>
<span class="type_a">4</span>
<span class="type_b">3</span>
</div>
jQuery:
$('.container .type_b').each(function(index) {
if ( $(this).hasClass('active') ) {
console.log($(this).index());
}
});
【问题讨论】:
标签: javascript jquery html indexing each