How to combine has () and gt ()

As of jQuery 1.4.2, I cannot figure out how to combine has () with: gt. I would like to select any <ul>

one that contains more than 3 <li>

s, so this is what I tried:

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

      

This works with the jQuery 1.2.6 library, but not 1.3.2 or 1.4.2. What for?

+2


a source to share


2 answers


Try using nth-child :

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

      



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

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

Sub>

+1


a source


Try .filter()



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

      

0


a source







All Articles