Order of items when targeting multiple items

I have a <ul></ul> with over 4000 <li></li> , and I am trying to select only their range. those. [50 - 100] .

When I target all li with jquery, for example: $('ul > li'); I get an array of length li 4000.

So, I'm trying to loop from 50 to 100 and aim each of them:

 for (var i = startCount; i <= stopCount; i++ ) { $('ul > li')[i].addClass('transparent'); } 

But when I settled on dev tools, I see that $('ul > li')[50] is at the bottom of ul . I expected it to be the 50th or 51st point in ul . Is there a way to find out if the UL List has an order?

+4
source share
1 answer

One way to achieve this is to use the jQuery slice () method.

slice() - Reduce the set of matched elements to the subset specified in the index range.

In your case, it will be:

 $('ul > li').slice(49, 100); // Targets the 50th to 100th ul > li elements only. 

JsFiddle example here.

+7
source

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


All Articles