从子div获取父div的索引 [英] Get index of parent div from child divs
本文介绍了从子div获取父div的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有几个div,在里面我有嵌套的div。我希望能够得到我的孩子div所在的父div的索引。即:如果我点击post5我会得到index1或如果我点击post4它仍然会得到索引1.
I have a few divs and inside them I have nested divs. I would like to be able to get the index of the parent div my child divs reside in. Ie: If I clicked "post5" I would get the index1 or If I clicked "post4" it would still get index 1.
Html:
<div class="more-content">
<div class="post">post 1</div>
<div class="post">post 2</div>
<div class="post">post 3</div>
</div>
<div class="more-content">
<div class="post">post 4</div>
<div class="post">post 5</div>
<div class="post">post 6</div>
</div>
<div class="more-content">
<div class="post">post 7</div>
<div class="post">post 8</div>
<div class="post">post 9</div>
</div>
Jquery:
$(".post").click(function() {
alert($(".post").parent.index(this));
});
推荐答案
以下情况应该有效:
$(".post").click(function() {
var index = $(this).parent().index(".more-content");
alert(index);
});
DEMO: http://jsfiddle.net/NDySY/
这篇关于从子div获取父div的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文