document.all与WEB标准

2019-06-03 15:43:14于丽


  上面的这个例子让你了解怎么访问文档中的一个特定元素,比如文档中有一个DIV
<div id="docid" name="docname"></div>,你可以通过这个DIV的ID,NAME或INDEX属性访问这个DIV:

document.all["docid"] 
document.all["docname"] 
document.all.item("docid") 
document.all.item("docname") 
document.all[7] 
document.all.tags("div")则返回文档中所有DIV数组,本例中只有一个DIV,所以用document.all.tags("div")[0]就可以访问了。 
  3、使用document.all[]

例3


[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
  4、标准DOM中的访问方法

  开头就说过document.all[]不符合WEB标准,那用什么来替代它呢?document.getElementById


Most third-party browsers are “strict standards” implementations, meaning that they implement W3C and ECMA standards and ignore most of the proprietary object models of Internet Explorer and Netscape.If the demographic for your Web site includes users likely to use less common browsers, such as Linux aficionados, it might be a good idea to avoid IE-specific features and use the W3C DOM instead. by Internet Explorer 6, we see that IE implements significant portions of the W3C DOM.

  这段话的意思是大多数第三方浏览器只支持W3C的DOM,如果你的网站用户使用其他的浏览器,那么你最好避免使用IE的私有属性。而且IE6也开始支持W3C DOM。

  毕竟大多数人还不了解标准,在使用标准前,你还可以在你的网页中用document.all[]访问文档对象前面写到WEB标准,今天继续WEB标准下可以通过getElementById(), getElementsByName(), and getElementsByTagName()访问DOCUMENT中的任一个标签:

  1、getElementById()

  getElementById()可以访问DOCUMENT中的某一特定元素,顾名思义,就是通过ID来取得元素,所以只能访问设置了ID的元素。