简体   繁体   中英

Best Way to Get All DOM Elements with jQuery

What's the best way to get all of the DOM elements on a page using jQuery?

Thanks,

DLiKS

Edit: This is for use in a script that grayscales an entire page using grayscale.js - http://james.padolsey.com/demos/grayscale/ . jQuery because I can! :P

var allOfThem = $('*');

你真的不需要jQuery:

var allOfThem = document.getElementsByTagName('*');

document.getElementsByTagName("*") will return all DOM elements as "actual" elements, with all their contents and properties and everything.

$('*') or $("body *") will return array of "jQuery objects", each only pointing on true element. To get the true element, you'll have to use the specific jQuery object.

Guess this difference is what causing this behavior of browser crashing when getting all elements vs. getting all jQuery objects.

It seems you want $("body *") , which is equivalent to document.documentElement.getElementsByTagName('*')

Weirdly, getElementsByTagName('*') seems to crash my Firefox/Firebug, while jQuery version works fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM