javascript - 使用jquery查找div的索引

标签 javascript jquery html indexing

这是我的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);
});

Fiddle

关于javascript - 使用jquery查找div的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849812/

相关文章:

python - HTML 不反射(reflect) Beautiful Soup 浏览器中的网页内容

javascript - AngularJS:$http响应太慢

javascript - 无法使用 jQuery 选择器访问单击事件

javascript - angularjs 是否适合大型以数据为中心的企业 Web 应用程序

javascript - 在鼠标悬停时淡出图片,然后淡入不同的图片

javascript - 如何使用不同名称的复选框更改 css?

html - 我似乎无法将标题和导航栏放在同一行上

javascript - JS : incrementing pages (page1. html, page2.html,page3.html...) 用于 window.replace

javascript - 如何在 AJAX 请求中设置 CORS

c# - JavaScript 中的 Div 可见性