简体   繁体   English

如何查找页面中的所有元素

[英]How to find all elements in a page

I know of two ways to find all the elements in the page. 我知道有两种方法可以找到页面中的所有元素。

document.getElementsByTagName('*') and document.all document.getElementsByTagName('*')document.all

Is there a better way or is one of these the best? 有更好的方式还是最好的方式之一?

I'm making an element selector (mostly for practice) and I want to know the most reliable way to get all of the elements in the page, to test against the selector. 我正在制作一个元素选择器(主要用于练习),我想知道获取页面中所有元素的最可靠方法,以测试选择器。

document.all is an outdated proprietary method from Microsoft. document.all是Microsoft过时的专有方法。 Don't use it. 不要使用它。

document.getElementsByTagName('*') is the W3C standard method of finding all the elements ina document - and surely the fastest - and works in Internet Explorer as well. document.getElementsByTagName('*')是W3C标准方法,用于查找文档中的所有元素 - 当然也是最快的 - 并且也可以在Internet Explorer中使用。

PS As someone is bound to chime in and provide a jQuery answer to this question, there's how to select all elements in a page using jQuery: jQuery('*'); PS因为某人必然会在这个问题中提供jQuery答案,所以如何使用jQuery选择页面中的所有元素: jQuery('*');

:-) :-)

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

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