【问题标题】:get order of child selected jquery获取子选择jquery的顺序
【发布时间】:2012-03-17 09:46:37
【问题描述】:

我想根据他们的订单为 ul 孩子制作不同的背景颜色,我怎样才能使用 jquery 获得孩子的顺序。

<ul>
<li>a</li>
    <li>b</li>   
     <li>c</li>
     <li>d</li>
     <li>e</li>
    <li>f</li>
 </ul>

$('ul li').bind('mouseover', function(e){

    //how can I get the order of the childre
    //when hover on c it shoul returns 3
    alert("child order ");
});

这是一个例子 http://jsfiddle.net/sHefJ/

【问题讨论】:

    标签: jquery children


    【解决方案1】:

    对于该 HTML 子集,使用...

    $(this).index() + 1
    

    jsFiddle.

    ...就足够了。

    我添加了1,因为您的示例中的编号系统似乎不是基于0

    【讨论】:

      【解决方案2】:

      以下将为您提供一个索引。所以 c 应该是 2,而不是 3!

      $(this).index();
      

      【讨论】:

        猜你喜欢
        • 2014-03-06
        • 2014-03-07
        • 2012-04-29
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        相关资源
        最近更新 更多