我有几个div,在里面我有嵌套的div。我希望能够得到我的孩子div所在的父div的索引。即:如果我点击“post5”我会得到index1或如果我点击“post4”它仍然会得到索引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));
});
答案 0 :(得分:11)
以下内容应该有效:
$(".post").click(function() {
var index = $(this).parent().index(".more-content");
alert(index);
});