Is there a better way to select tables based on how many rows they have through jQuery?

I want to capture a group of tables that have more than X rows with jQuery. I am currently doing something similar to:

    $("table").each(function(){
 if($(this).find("tr").length > x){
  tableArray[tableArray.length] = $(this);
 }
});

and then acting on the tableArray elements.

Is there a better way to get these tables, maybe a good selector that I missed?

thank

+3
source share
3 answers

Try using the :hasselector:

$('table:has(tr:eq('+x+'))');

x. , :eq() , , x 1, , 2 , .

EDIT :has , :nth-child :eq. .has() ( ) :

alert($('table').has("tr:nth-child(2)").length)​

, , , nth-child, , :eq.

- .

+3

http://api.jquery.com/nth-child-selector/

$("table tr:nth-child(" + (x + 1) + ")").parent();

, . : ".parent()" .

0

I think your path is fine, to be honest.

One alternative way would be to use a selector in conjunction with : ask jQuery for . This says "find all tables whose elements have 4 or more children." Since child elements are usually elements, this will do the trick. :has nth-child$("tbody:has(:nth-child(4)))...tbodytbodytr

0
source

Source: https://habr.com/ru/post/1749773/


All Articles