jquery查找元素的位置

发布于 2024-08-18 01:32:07 字数 219 浏览 4 评论 0原文

<ul>
   <li>one</li>
   <li>element</li>
   <li>text</li>
   <li>val</li>
</ul>

我怎样才能获得点击的liul中的位置?

<ul>
   <li>one</li>
   <li>element</li>
   <li>text</li>
   <li>val</li>
</ul>

how can i get the position in the ul of the clicked li ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南笙 2024-08-25 01:32:07

我认为这会为你做到这一点:

$("li").click(function () {

   alert($(this).index());

});

请注意,index()函数返回jquery集合中项目的索引。如果页面上有多个列表,请确保选择器仅选择所需的列表项。

I think this will do it for you:

$("li").click(function () {

   alert($(this).index());

});

Note that the index() function returns the index of the item in the jquery collection. If you have multiple lists on the page, make sure your selector only selects the list items that you want.

茶色山野 2024-08-25 01:32:07
$('ul li').click(function() {
    alert(   $(this).parent().find('li').index(this)  );
});

参考

我只测试了一个

    。如果您在多个

      上执行此操作,则需要 .each
$('ul li').click(function() {
    alert(   $(this).parent().find('li').index(this)  );
});

Reference

I only tested with one <ul>. You'll need to .each if you're doing this on multiple <ul>s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文