html document
HTML DOM 节点的概念
HTML DOM(Document Object Model)文件对象模型中,所有的元素都是一个独立的节点:概念如下:
- 文件(document)本身是一个文书节点
- 所有的HTML元素称为元素节点
- 所有的HTML属性称为属性节点
- 文本文件里的HTML元素称为文本文件节点
- 注释称为注释节点
什么是文本对象(Document Object)?
当一个HTML文件下载到web浏览器上后,该文件便成为一个文件对象。
文件对象是HTML文件的根节点,也是所有其他节点(元素节点、文件节点、属性节点、注释节点)的头节点。
文件节点提供能够到达所有节点的属性和方法。
注:文件对象是window对象的一部分,能够通过window.document进行访问。
不同浏览器的对应情况
| 对象 | |||||
| Document | yes | yes | yes | yes | yes |
文件对象的属性及方法
能够在HTML文档中使用的属性与方法
- document.activeElement返回文档中当前聚焦的元素
- document.addEventListener()给文档添加一个事件处理程序
- document.adoptNode()从另一个文档接收一个节点
- document.anchors()返回同一文档中所有带有name属性的a元素的集合体
- document.baseURI获得节点的根本URI
- document.body 设置或返回文档的body元素
- document.close()关闭之前打开的输出流
- document.cookie设置或返回当前文档的cookie
- document.createAttribute()创建属性节点
- document.createComment()创建注释节点
- document.createDocumentFragment()创建虚拟节点对象
- document.createElement();创建元素节点
- document.createTextNode()创建文本节点
- document.doctype返回与文档相关的文档类型
- document.documentElement返回文档的根元素
- document.documentURI设置或返回文档的保存地址
- document.domain返回当前文档所在的站点域名
- document.embeds返回当前文档中所有embed元素的列表
- document.forms返回当前文档中所有form元素的列表
- document..getElementById()使用指定的id属性值获取指定的id元素
- document.getElementByclassName()使用指定的calss属性值返回相关元素的集合(节点列表)
- document.getelementsbyname()使用指定的name属性值获取文档中相关元素的集合
- document.getElementsByTagName()使用指定的元素返回一个节点列表集合
- document.hasFocus()判断文档中是否有可聚焦的元素
- document.head返回当前文档中的head元素
- document.images返回当前文档中包含了所有img元素的集合
- document.implementation返回处理当前文档的DOMimplementation对象
- document.importNode()从另一个文档复制一个节点
- document.inputEncoding返回当前文档的字符编码
- document.lastModified返回当前文档最后修改的日期时间
- document.links返回当前文档中带有href属性的a元素和area元素的集合
- document.normalize()删除空文本节点,合并相邻文本节点,使文档正规化
- document.normalizeDocument()删除空文本节点,合并相邻文本节点,使文档正规化
- document.open打开输出流
- document.querySelector()使用指定的css选择器获取与之对应的第一个元素
- document.querySelectorAll()使用指定的css选择器获取与之对应的所有元素
- document.readyState返回文档的状态
- document.referrer返回下载的当前文档的URL
- document.removeEventListener()取消给文档添加的事件处理程序
- document.renameNode()给指定的节点改换名称
- document.scripts返回当前文档中包含了所有script元素的集合
- document.strictErrorChecking设置或返回是否强制执行错误检查
- document.title设置或返回文档的标题title
- document.url返回文档的绝对路径
- document.write()文档中书写字符串
- document.writeln()文档中书写最后带有换行符的字符串
注意!!
HTML文档中应该回避使用的节点对象的属性及方法:
W3C DOM的核心里,文档对象从节点对象继承了很多属性及方法。这些属性和方法不能在文档中使用。
| 属性/方法 | 回避原因 | |
|---|---|---|
| document.attributes | 文档没有属性 | |
| document.hasAttributes() | 文档没有属性 | |
| document.nextSibling | 文档没有同级文档(没有兄弟姐妹) | |
| document.nodeName | 为#document | |
| document.nodeType | 为9(DOCUMENT_NODE) | |
| document.nodeValue | 文档没有节点属性 | |
| document.ownerDocument | 文档没有所属文档 | |
| document.ownerElement | 文档没有所属元素 | |
| document.parentNode | 文档没有上级节点 | |
| document.previousSibling | 文档没有同级文档 | |
| document.textContent | 文档没有文本内容 |
|

浙公网安备 33010602011771号