1. document.layers是Netscape 4.x的专有属性
是Netscape 4.x专有的属性,是一个代表所有由储如<div><layer>等定位了的元素的数组。通常也是用<div>或<layer>对象的id属性来引用的,但是这里面不包含除此以外的其它元素。
2. document.all
是IE 4.0及以上版本的专有属性,是一个表示当前文档的所有对象的娄组,不仅包括页面上可见的实体对象,还包括一些不可见的对象,比如html注释等等。在document.all数组里面,元素不分层次,是按照其在文档中出现的先后顺序,平行地罗列的。所以可以用数字索引来引用到任何一个元素。但比较常用的是用对象id来引用一个特定的对象,比如document.all["element"]这样。
其实这两个属性没什么可比性,大概你经常看到他们同时出时,这有一个历史原因。在第四代浏览器出现的时候,标准相当混乱,Netscape和微软分别推出了它们的Navigator 4.x和IE 4.0,这两个浏览器的巨大差异,也使开发者面临了一个使网页跨浏览器兼容的噩梦。而document.layer和document.all分别是两者一个最显著的标志,为了确定浏览者使用的什么浏览器,通常用是否存在document.layers和document.all来判断。
新的统一的标准用document.getElementById等系列方法来引用DOM对象,而且Netscape 6.0以后放弃了layers特征,虽然IE继续保留了document.all,但这最终没有成为DOM标准的一部分。希望document.layers和document.all能够早日作古,让标准早日深入人心!
3. document.getElementById
最初被定义为 HTMLDocument (HTML DOM)接口的一个成员,但是在后来的 2 级 DOM 中移入到 Document (XML DOM)接口中。
document.getElementById属于host object,它是一个function,但是它并没有被定义在ECMAScript中而是DOM接口的一部分。
最新版的chrome和opera浏览器也支持document.all 引自
document.layer和document.all都是过时的写法,现在一般用document.getElementById
火狐官方文档 https://developer.mozilla.org/en-US/docs/Mozilla_Web_Developer_FAQ#JavaScript_doesn.E2.80.99t_work.21_Why.3F
Some proprietary document objects such as document.all
and document.layers
are not part of the W3C DOM and are not supported in Mozilla. (There is partial undetectable support for document.all
, though, in newer versions of Mozilla. However, that functionality only exists for compatibility with sites authored specifically for IE. You should not rely on Mozilla’s document.all
support on new pages.) The method document.getElementById()
can be used instead.
In the Standards mode Mozilla does not generate implicit top-level JavaScript variable bindings for elements with the id
or name
attribute. The correct way to access an element by id is to call the document.getElementById()
method with the id as a string as the argument.
Also, old client sniffers can shut out new browsers. The point of having a common API (the W3C DOM) is interoperability, and checking for a particular browser defeats that purpose. When working with the DOM, it is better to check for the existence of the methods and objects you are planning on using. For example, the existence of document.getElementById()
can be checked as follows:
if(document.getElementById) { /* code that uses document.getElementById() */ }
通常用下面的if-else判断浏览器类型
if (document.layers) { } else if (document.all) {
} else if (document.getElementById) { }
For Opera : if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP"; Internet Explorer e.g. IE4 upwards : if (document.all) type="IE"; For Netscape version 4 : if (document.layers) type="NN"; Mozila e.g. Netscape 6 upwards if (!document.all && document.getElementById) type="MO";
http://www.jb51.net/article/28283.htm
http://www.webconcerns.co.uk/javascript/dhtml/dhtml_page.asp