Javascript 在jQuery中基于子对象获取父对象的索引

Javascript 在jQuery中基于子对象获取父对象的索引,javascript,jquery,Javascript,Jquery,当前,下面的代码返回一个可见的父/同级div数组,其类为。三分之一,但我需要根据返回的数组找到单击文章的位置。例如,post-1320的索引为0,post-1321返回1,post-1324返回2,以此类推 将.index('.+unqiuePostClass[1])添加到代码末尾会返回-1,因为它找不到类 HTML <div class="one-third"> <article class="post post-1320">post-1320</arti

当前,下面的代码返回一个可见的父/同级div数组,其类为
。三分之一
,但我需要根据返回的数组找到单击文章的位置。例如,post-1320的索引为0,post-1321返回1,post-1324返回2,以此类推

.index('.+unqiuePostClass[1])
添加到代码末尾会返回-1,因为它找不到类

HTML

<div class="one-third">
    <article class="post post-1320">post-1320</article>
</div>
<div class="one-third">
    <article class="post post-1321">post-1321</article>
</div>
<div class="one-third" style="display:none;">
    <article class="post post-1322">post-1322</article>
</div>
<div class="one-third" style="display:none;">
    <article class="post post-1323">post-1323</article>
</div>
<div class="one-third">
    <article class="post post-1324">post-1324</article>
</div>
<div class="one-third">
    <article class="post post-1325">post-1325</article>
</div>
<div class="one-third">
    <article class="post post-1326">post-1326</article>
</div>

<div class="clear"></div>
结果

0: div.one-third
1: div.one-third
2: div.one-third
3: div.one-third
4: div.one-third
一个更好地演示了我的问题的JSFIDLE:

使用并传入与之相关的元素选择器:

$(this).parent().index('div.one-third:visible')

您可以在
:visible
元素上使用
.index()
,元素的类为
.1/3

$('.post')。单击(函数(){
var-index=$('.1-third:visible>.post')。索引(this);
控制台日志(索引);
});
。三分之一{
宽度:33.3333%;
浮动:左;
}
.邮政{
背景:红色;
保证金:5px;
填充物:5px;
光标:指针;
}
.clear{clear:both;}

1320年后
1321年后
1322年后
1323年后
1324年后
1325年后
1326年后

如果单击类为
的文章。post
则确定所单击文章的唯一post类,然后选择具有此唯一类的所有文章-这将精确生成所单击的文章。为什么要绕道?
$(this).parent().index('div.one-third:visible')