Javascript document.all 与 document.getElementById
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2408424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.all vs. document.getElementById
提问by coderex
When should you use document.allvs. document.getElementById?
什么时候应该使用document.allvs. document.getElementById?
回答by Phil Rykoff
document.allis a proprietary Microsoft extension to the W3C standard.
document.all是 Microsoft 对 W3C 标准的专有扩展。
getElementById()is standard - use that.
getElementById()是标准的 - 使用它。
However, consider if using a js library like jQuerywould come in handy. For example, $("#id")is the jQuery equivalent for getElementById(). Plus, you can use more than just CSS3selectors.
但是,请考虑使用像jQuery这样的 js 库是否会派上用场。例如,$("#id")jQuery 等效于getElementById(). 此外,您可以使用的不仅仅是 CSS3选择器。
回答by Marcel Korpel
document.allis veryold, you don't have to use it anymore.
document.all是很老,你不必使用它了。
To quote Nicholas Zakas:
For instance, when the DOM was young, not all browsers supported getElementById(), and so there was a lot of code that looked like this:
例如,在 DOM 还年轻的时候,并不是所有的浏览器都支持 getElementById(),所以有很多代码是这样的:
if(document.getElementById){ //DOM
element = document.getElementById(id);
} else if (document.all) { //IE
element = document.all[id];
} else if (document.layers){ //Netscape < 6
element = document.layers[id];
}
回答by Kevin Fegan
Actually, document.allis only minimallycomparable to document.getElementById. You wouldn't use one in place of the other, they don't return the same things.
事实上,document.all只有最低限度媲美document.getElementById。你不会用一个代替另一个,它们不会返回相同的东西。
If you were trying to filter through browser capabilities you could use them as in Marcel Korpel's answerlike this:
如果您试图过滤浏览器功能,您可以像Marcel Korpel 的回答一样使用它们,如下所示:
if(document.getElementById){ //DOM
element = document.getElementById(id);
} else if (document.all) { //IE
element = document.all[id];
} else if (document.layers){ //Netscape < 6
element = document.layers[id];
}
But, functionally, document.getElementsByTagName('*')is more equivalent to document.all.
但是,在功能上,document.getElementsByTagName('*')更等同于document.all.
For example, if you were actually going to use document.allto examine all the elements on a page, like this:
例如,如果您实际上要使用document.all检查页面上的所有元素,如下所示:
var j = document.all.length;
for(var i = 0; i < j; i++){
alert("Page element["+i+"] has tagName:"+document.all(i).tagName);
}
you would use document.getElementsByTagName('*')instead:
你会document.getElementsByTagName('*')改用:
var k = document.getElementsByTagName("*");
var j = k.length;
for (var i = 0; i < j; i++){
alert("Page element["+i+"] has tagName:"+k[i].tagName);
}
回答by Marcos Placona
document.all()is a non-standard way of accessing DOM elements. It's been deprecated from a few browsers. It gives you access to all sub elements on your document.
document.all()是一种访问 DOM 元素的非标准方式。它已被一些浏览器弃用。它使您可以访问文档中的所有子元素。
document.getElementById()is a standard and fully supported. Each element have a unique id on the document.
document.getElementById()是标准的并且完全受支持。每个元素在文档上都有一个唯一的 id。
If you have:
如果你有:
<div id="testing"></div>
Using
使用
document.getElementById("testing");
Will have access to that specific div.
将有权访问该特定 div。
回答by Multiversum
document.querySelectorAll(and its document.querySelector()variant that returns the first found element) is much, much more powerful. You can easily:
document.querySelectorAll(及其document.querySelector()返回第一个找到的元素的变体)功能要强大得多。您可以轻松:
- get an entire collection with
document.querySelectorAll("*"), effectively emulating non-standarddocument.allproperty; - use
document.querySelector("#your-id"), effectively emulatingdocument.getElementById()function; - use
document.querySelectorAll(".your-class"), effectively emulatingdocument.getElementsByClassName()function; - use
document.querySelectorAll("form")instead ofdocument.forms, anddocument.querySelectorAll("a")instead ofdocument.links; - and perform any much more complex DOM querying (using any available CSS selector) that just cannot be covered with other document builtins.
- 使用 获取整个集合
document.querySelectorAll("*"),有效地模拟非标准document.all属性; - 使用
document.querySelector("#your-id"),有效模拟document.getElementById()功能; - 使用
document.querySelectorAll(".your-class"),有效模拟document.getElementsByClassName()功能; - 使用
document.querySelectorAll("form")代替document.forms,document.querySelectorAll("a")代替document.links; - 并执行其他文档内置函数无法覆盖的任何更复杂的 DOM 查询(使用任何可用的 CSS 选择器)。
Unified querying API is the way to go. Even if document.allwould be in the standard, it's just inconvenient.
统一查询 API 是必经之路。即使document.all符合标准,也很不方便。
回答by DOK
Specifically, document.allwas introduced for IE4 BEFORE document.getElementByIdhad been introduced.
具体来说,document.all是为 IE4document.getElementById引入的。
So, the presence of document.allmeans that the code is intended to support IE4, or is trying to identify the browser as IE4 (though it could have been Opera), or the person who wrote (or copied and pasted) the code wasn't up on the latest.
因此,存在document.all意味着该代码旨在支持 IE4,或者试图将浏览器识别为 IE4(尽管它可能是 Opera),或者编写(或复制和粘贴)代码的人未注册在最新的。
In the highly unlikely event that you need to support IE4, then, you do need document.all(or a library that handles these ancient IE specs).
在极不可能的情况下,您需要支持 IE4,然后,您确实需要document.all(或处理这些古老的 IE 规范的库)。
回答by Michael Biermann
According to Microsoft's archived Internet Explorer Dev Center, document.allis deprecated in IE 11 and Edge!
根据Microsoft 存档的 Internet Explorer Dev Center,document.all在 IE 11 和 Edge 中已弃用!
回答by mike nelson
document.allworks in Chrome now (not sure when since), but I've been missing it the last 20 years.... Simply a shorter method name than the clunky document.getElementById. Not sure if it works in Firefox, those guys never had any desire to be compatible with the existing web, always creating new standards instead of embracing the existing web.
document.all现在在 Chrome 中工作(不知道从什么时候开始),但在过去的 20 年里我一直在想念它......只是一个比笨重的document.getElementById. 不确定它是否适用于 Firefox,这些人从来没有任何希望与现有网络兼容,总是创建新标准而不是拥抱现有网络。

