这是我的html代码:
<div class="more-content">
<input type="text" />
<div class="post">post 1</div>
<div class="post">post 2</div>
<div class="post">post 3</div>
</div>
这是jquery:
$(".post").click(function () {
var index = $(this).index();
alert(index);
});
我不想在尝试获取主 div 内的 div 索引时考虑输入文本。因此,单击帖子 3 必须显示 2 而不是 3。如何做到这一点?
这里是 jsfidle http://jsfiddle.net/NDySY/16/
最佳答案
您可以这样使用index()
:
$(".post").click(function() {
var index = $(this).index('.post');
alert(index);
});
关于javascript - 使用jquery查找div的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849812/