Scrolling DOM HTML with jQuery

I want to iterate over all the elements in the DOM structure of an html page using jQuery. Basically, only those elements in body tags are needed.

The reason I want to do this is because I want to get the positions of each element in the body structure.

I googled around and I did not find the answer to my question.

Your help is greatly appreciated.

+3
source share
3 answers
$('body *').each(function() {
    // do stuff
});

If you have built-in tags <script>, etc., you can add a selector :notto exclude those:

$('body *:not(script, style, noscript)').each(function() {
    // do stuff
});

As pointed out in the comments, and jQuery is all a docs selector , this probably won't work very well. I have not done any empirical tests, but this could improve:

$('body').find('*').not('script, style, noscript').each(...);
+9
source

, getElementsByTagName jQuery:

$(document.body.getElementsByTagName("*")).each(function () {
     // Do something here
});

* - , jQuery:

: "" "" , , .

http://api.jquery.com/all-selector/

, , , getElementsByTagName:

$(document.body).find("*");
$("body").find("*");
$("*", document.body);
+5

Use the whole selector :

$("body *").each(...)
0
source

All Articles