获取嵌套列表项的索引

发布于 2024-11-11 18:37:45 字数 577 浏览 4 评论 0原文

我正在尝试获取嵌套列表的索引。

http://jsfiddle.net/5zJD8/12/

我举了一个例子。

当您单击列表项时,您会看到以下内容,

list item 0
  list item 0
  list item 1
list item 1
list item 2

我希望它执行以下操作:

list item 0
  list item 1
  list item 2
list item 3
list item 4

有人能给我指出正确的方向吗?我希望我已经解释得足够好了。

如果演示无法加载,则代码如下,当您此时单击列表项时,它只会附加 ID。

$('li').click( function() {
   var liIndex = $(this).index();
    $(this).children().append(liIndex);
});

I'm trying to get the index of a nested list.

http://jsfiddle.net/5zJD8/12/

I've put an example up.

At the moment when you click a list item you get this

list item 0
  list item 0
  list item 1
list item 1
list item 2

I want it to do this:

list item 0
  list item 1
  list item 2
list item 3
list item 4

Could someone point me in the right direction please? I hope I've explained this well enough.

Here's the code if the demo wont load, It just appends the ID when you click a list item at the moment.

$('li').click( function() {
   var liIndex = $(this).index();
    $(this).children().append(liIndex);
});

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

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

发布评论

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

评论(2

抽个烟儿 2024-11-18 18:37:45

试试这个:

$('li').click( function() {
   var liIndex = $(this).index('li');
    $(this).children().append(liIndex);
});

http://jsfiddle.net/5zJD8/36/

您可以在此处查找更多信息: http://api.jquery.com/index/

Try this:

$('li').click( function() {
   var liIndex = $(this).index('li');
    $(this).children().append(liIndex);
});

http://jsfiddle.net/5zJD8/36/

you may look for more info here : http://api.jquery.com/index/

水波映月 2024-11-18 18:37:45

这做同样的事情,但稍微省力一点:

$('li').each(function(liIndex) {
    $(this).click( function() {
         $(this).children().append(liIndex);
    });
});

This does the same thing, but a bit less effort:

$('li').each(function(liIndex) {
    $(this).click( function() {
         $(this).children().append(liIndex);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文