some content

Does jQuery implicit loop go both ways?

Turns off jQuery "implicit loop" goes both ways:

<div class="classOne"> some content </div> <div class="classOne"> some content 2 </div> [...] $(function() { $('hello world').prependTo($('.classOne')); }) 

in this case, the loop will be in the $('.classOne') section - hello world will be added to both sections.

I also tried

  <div class="classOne"> some content </div> <div class="classOne"> some content 2 </div> <div class="classTwo"> <a href="http://www.google.com">hello Google</a> </div> <div class="classTwo"> <a href="http://www.yahoo.com">hello Yahoo</a> </div> [...] $(function() { $('.classTwo').prependTo($('.classOne')); }) 

and there will be "nested loops" ... so 2 links will be added to both Divs

so I think if we

 $('.classOne').prepend($('.classTwo')).prepend($('.classThree')) 

then it will be like 3 nested loops? Is there a nesting rule, and which one is internal and which is external? And what is the inner loop / outer loop if it

 $('.classOne').prependTo($('.classTwo')).prependTo($('.classThree')) 

?

+4
source share
1 answer

Each predecessor .prependTo() will be added to each element passed in, here you see the real jQuery kernel code . Since you are passing the jQuery object to .prependTo() , it scans each of these elements and adds a cloned version of each object in the chain preceding the prefile.

So, each .prependTo() = one for loop (when passing a jQuery object), but they are not nested. The results of one of them are simply passed to the next, but it receives an array that receives the elements superimposed on it, you can use .end() to return to the previous array, for example.

I apologize if this is not a crystal clear explanation, I understand that it is a little strange to think about the chain ... but if you can point out any questions that this leaves in the comments, I will try and update to address any confusion / partially missed.

+2
source

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


All Articles