开发者

How to combine has() and gt()

With jQuery 1.4.2, I can't figure out how to combine has() with :gt. I'd like to select any <ul> which contains more than 3 <li>s, so here's what I've tried:

$(document).ready(function(){  
  $("ul.collapse:has(li:gt(2))")  
    .each( function() {  
 开发者_JS百科      $(this).css("border", "solid red 1px");  
  });  
});  

This does work with the 1.2.6 jQuery library, but not 1.3.2 or 1.4.2. Why?


Try using nth-child:

$('ul').has('li:nth-child(3)').css('border', 'solid red 1px');

Working Example: http://jsbin.com/opape3

This selector didn't work for some reason: $("ul.collapse:has(li:nth-child(3))")


Try .filter()

$("ul").filter(function(){
    return $(this).find("li").length > 4;
}).each(function(){
    // your code   
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜