【问题标题】: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 的索引