Best Way to Get All DOM Elements with jQuery

吃可爱长大的小学妹 提交于 2019-12-21 19:56:18

问题


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


回答1:


var allOfThem = $('*');

You don't really need jQuery for this:

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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/4024027/best-way-to-get-all-dom-elements-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!