如何获得兄弟姐妹之间的元素编号/索引 [英] How to get the element number/index between siblings

查看:83
本文介绍了如何获得兄弟姐妹之间的元素编号/索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取所选元素的子代号,例如

How can you get the child number of the selected element, e.g.

<ul>
    <li>First child</li>
    <li>Second</li>
    <li id="selected">Third child</li>
    <li>Fourth child</li>
</ul>

使用类似这样的内容:

$('#selected').childNumber();

关于子编号,它的父包装应该返回"3".

That should return "3" for it's parent wrap, regarding the child number.

推荐答案

您可以使用 .index() .

var selectedIndex = $("#selected").index() + 1;

由于您已经为li指定了ID,因此无需使用带有ul的元素选择器.您可以直接为li元素使用id选择器.

Since you have given an id for the li there is no need for the element selector with ul. You can directly use the id selector for the li element.

这篇关于如何获得兄弟姐妹之间的元素编号/索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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