DOM & BOM
DOM
DOM (Document Object Model) is a tree-like structure that represents the structure of a document.
W3C推出的标准化DOM可以让任何一种程序设计语言对使用任何一种标记语言编写出来的任何一份文档进行操控。
W3C对DOM的定义是:“一个与系统平台和编程语言无关的接口,程序和脚本可以通过这个接口动态地访问和修改文档的内容、结构和样式。”
- Tree of nodes/elements created by the browser
- JavaScript can be used to read/write/manipulate the DOM
- Object Oriented Representation
node
node的类型:
Document:整个文档树的顶层节点DocumentType:doctype标签(exp.<!DOCTYPE html>)Element:网页的各种HTML标签(exp.<body>、<a>等)Attr:网页元素的属性(exp.class="right")Text:标签之间或标签包含的文本Comment:注释DocumentFragment:文档的片段
![]()
除了根节点,其他节点都有三种层级关系。
- 父节点关系(parentNode):直接的那个上级节点
- 子节点关系(childNodes):直接的下级节点
- 同级节点关系(sibling):拥有同一个父节点的节点
node接口
属性
Summary
element.nodeType:节点类型1:Element2:Attr3:Text8:Comment9:Document10:DocumentType11:DocumentFragment
element.nodeName:节点名称文档节点(document):#document元素节点(element):大写的标签名属性节点(attr):属性的名称文本节点(text):#text文档片断节点(DocumentFragment):#document-fragment文档类型节点(DocumentType):文档的类型注释节点(Comment):#comment
element.nodeValue:节点的文本值(只有文本节点,注释节点,属性节点有)element.textContent: 当前节点和所有后代节点的文本内容element.ownerDocument: 所属文档
element.parentNode: 父节点.对于一个节点来说,它的父节点只可能是三种类型:元素节点(element)、文档节点(document)和文档片段节点(documentfragment)element.childNodes: returns the child nodes of the elementelement.children: returns the children of the elementelement.childElementCount: returns the number of child elements of the elementelement.firstChild: returns the first child node of the elementelement.firstElementChild: returns the first element child of the elementelement.lastChild: returns the last child node of the elementelement.lastElementChild: returns the last element child of the elementelement.nextSibling: returns the next sibling of the elementelement.nextElementSibling: returns the next element sibling of the elementelement.previousSibling: returns the previous sibling of the elementelement.previousElementSibling: returns the previous element sibling of the element
方法
Summary
element.createElement(tagName): creates an element of the specified tag nameelement.createTextNode(text): creates a text node with the specified textelement.appendChild(child): appends the specified child to the elementelement.insertBefore(newChild, referenceChild): inserts the specified child before the specified reference childelement.removeChild(child): removes the specified child from the elementelement.replaceChild(newChild, oldChild): replaces the specified old child with the specified new childelement.hasChildNodes(): returns true if the element has child nodeselement.cloneNode(deep): returns a duplicate of the elementelement.contains(otherNode): returns true if the element contains the specified other nodeelement.normalize(): normalizes the elementelement.isEqualNode(otherNode): returns true if the element is the same node as the specified other nodeelement.getrootNode(): returns the root node of the document
Document节点
Summary
document: the root node of the DOM treedocument.body: the body element of the documentdocument.head: the head element of the documentdocument.documentElement: the root element of the documentdocument.title: the title of the documentdocument.location: the location of the documentdocument.URL: the URL of the documentdocument.referrer: the referrer of the documentdocument.cookie: the cookie of the documentdocument.lastModified: the last modified date of the documentdocument.characterSet: the character set of the documentdocument.dir: the direction of the documentdocument.doctype: the doctype of the documentdocument.all: the collection of all elements in the documentdocument.forms: the collection of all forms in the documentdocument.links: the collection of all links in the documentdocument.images: the collection of all images in the document- ...
Selectors
Summary
document.querySelector(selector): returns the first element that matches the specified selectordocument.querySelectorAll(selector): returns a collection of elements that match the specified selectordocument.getElementById(id): returns the element with the specified iddocument.getElementsByTagName(tagName): returns a collection of elements with the specified tag namedocument.getElementsByClassName(className): returns a collection of elements with the specified class namedocument.getElementsByName(name): returns a collection of elements with the specified name
Attributes
Summary
element.getAttribute(name): returns the value of the specified attributeelement.getAttributeNode(name): returns the specified attribute nodeelement.setAttribute(name, value): sets the specified attribute to the specified valueelement.hasAttribute(name): returns true if the element has the specified attributeelement.hasAttributes(): returns true if the element has attributeselement.removeAttribute(name): removes the specified attribute