简体   繁体   English

使用jQuery获取所有DOM元素的最佳方法

[英]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? 使用jQuery在页面上获取所有DOM元素的最佳方法是什么?

Thanks, 谢谢,

DLiKS DLiKS

Edit: This is for use in a script that grayscales an entire page using grayscale.js - http://james.padolsey.com/demos/grayscale/ . 编辑:这适用于使用grayscale.js灰色整个页面的脚本 - http://james.padolsey.com/demos/grayscale/ jQuery because I can! jQuery,因为我可以! :P :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. document.getElementsByTagName("*")将所有DOM元素作为“实际”元素返回,包含其所有内容和属性以及所有内容。

$('*') or $("body *") will return array of "jQuery objects", each only pointing on true element. $('*')$("body *")将返回“jQuery对象”数组,每个对象仅指向true元素。 To get the true element, you'll have to use the specific jQuery object. 要获得真正的元素,您必须使用特定的jQuery对象。

Guess this difference is what causing this behavior of browser crashing when getting all elements vs. getting all jQuery objects. 猜测这种差异是导致浏览器在获取所有元素与获取所有jQuery对象时崩溃的原因。

It seems you want $("body *") , which is equivalent to document.documentElement.getElementsByTagName('*') 看起来你想要$("body *") ,相当于document.documentElement.getElementsByTagName('*')

Weirdly, getElementsByTagName('*') seems to crash my Firefox/Firebug, while jQuery version works fine 奇怪的是, getElementsByTagName('*')似乎崩溃我的Firefox / Firebug,而jQuery版本工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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