简体   繁体   中英

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() {  
       $(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   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM