确定其父级中的子级索引 [英] Determining child index in it's parent

查看:66
本文介绍了确定其父级中的子级索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定元素在其父元素中的索引?假设您具有以下DOM结构,并且已为子div设置了 click 事件监听器.单击它们中的每个时,我想知道它是有关父div的索引.

How can I determine index of an element in it's parent? Assume you have following DOM structure and you have set a click event listener for child divs. When each of them is clicked, I want to know it's index regarding the parent div.

<div class="parent">
    <div class="child">...</div>
    <div class="child">...</div>
    <div class="child">...</div>
    <div class="child">...</div>
</div>

推荐答案

要获取其父元素(实际上是兄弟姐妹)中元素的索引,请使用

To get an index in of an elements in its parent (amongst siblings really) use .index() without any parameters, for example:

$(".child").click(function() {
  alert("Index: " + $(this).index());
});

您可以在此处进行测试.

这篇关于确定其父级中的子级索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆