【问题标题】:What is the best way to select an element if the element's index is over 3?如果元素的索引超过 3,那么选择元素的最佳方法是什么?
【发布时间】:2011-09-05 13:13:15
【问题描述】:

伪代码

$("#cool ul li.active:eq( > 3)") { // selector if the active li is over 3
     $('#cool ul').animate({right: '+=984'},0);
};

如果 li 超过 3,最好的选择方法是什么?

【问题讨论】:

  • 请您改进问题标题。
  • 抱歉问题标题 - 我将其更改为更常见的用途。

标签: javascript jquery indexing selector equals


【解决方案1】:

:gt selector

if ($("#cool li:gt(3)").hasClass('active')) { 
     $('#cool ul').animate({right: '+=984'},0);
};

编辑:第一次更正,以为我错了,弄错了,应该再次更正:P

【讨论】:

    【解决方案2】:
    if($("#cool ul li:gt(2)").filter('.active').length === 1) {
      $('#cool ul').animate({right: '+=984'},0);
    };
    

    编辑: 更新代码以假设 1 .active li
    编辑 2: 暂时忘记 :gt() 使用基于 0 的索引

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 2020-11-30
      • 2017-05-26
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多