如何查找页面中的所有元素 [英] How to find all elements in a page
问题描述
我知道有两种方法可以找到页面中的所有元素。
I know of two ways to find all the elements in the page.
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
是Microsoft过时的专有方法。不要使用它。
document.all
is an outdated proprietary method from Microsoft. Don't use it.
document.getElementsByTagName('*')
是找到所有的W3C标准方法文档中的元素 - 当然也是最快的 - 也可以在Internet Explorer中使用。
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.
PS 因为某人必然会加入并提供这个问题的jQuery答案,如何使用jQuery选择页面中的所有元素: jQuery('*');
P.S. 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('*');
: - )
这篇关于如何查找页面中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!