JQuery alternative to $ .each

I found that $ .each is very slow and creates problems for web pages if it contains many different jQuery effects.

I would like to know if there is a good alternative to $ .each, for example:

$('ul li').each(function() { var singleLi = $(this); }); 

without freezing, how can I do the same without using each?

Thanks.

+7
source share
4 answers

Similarly, without each

 $('#list li').hover(function() { $('#myDiv').stop().fadeIn(400); }, function() { $('#myDiv').stop().fadeOut(400); }) }); 
+6
source

If you want the actual alternative to be $ .each (), just use the for loop:

 var liElements = $('ul li'); for (var i = 0; i < liElements.length; ++i) { var li = liElements[i]; // ... lots of various jQuery effects ... } 

Suggestions that you can skip ".each ()" and just use ".hover ()" are correct, but they miss the point: these jQuery procedures will execute ".each ()" internally anyway.

I doubt that the transition from $ .each () to the for loop will itself be of great significance.

+24
source

yes ... just add hovering to all li

 $('#list li').hover(function() { $('#myDiv').fadeIn(400); }, function() { $('#myDiv').fadeOut(400); }); $('#myDiv').fadeOut(400); 
+3
source

Since the identifier must be unique, the whole script must not be changed if .each() is deleted.

$.hover() doesn't mean much.

 $('#list li').hover(function() { $('#myDiv').fadeIn(400); }, function() { $('#myDiv').fadeOut(400); }); $('#myDiv').fadeOut(400); 
+2
source

All Articles