JavaScript DOM is operating web interface, called the "Document Object Model" (Document Object Model). It is the role of the web page into a JavaScript object, which can perform various operations (such as add or delete content) script. DOM minimum constituent units, called nodes (node), a common type of node, there are four:
- Document: top node of the entire document tree.
- Element: page of various labels, such as
<body>
.- Text: labels or tags contain text.
- Attr: page element attributes, such as
class="right"
.
One, Node
All DOM node objects inherit the Node
interface has some common properties and methods. This is the basis DOM operations.
1.1 Object Properties
baseURI
Property returns a string that represents the absolute path of the current page (browser URL path on the opposite page is calculated based on the property). The value of this attribute is generally by the current URL URL (iewindow.location
the decision attribute). HTML can be used to<base>
label changes the value of the attribute.isConnected
Property returns a Boolean value that indicates whether the current node in the document.nodeType
Property returns an integer value representing the type of the current node. Node.DOCUMENT_XXX defined by the above-described various types.nodeName
Property returns a string representing the name of the current node.nodeValue
Property returns a string that represents the current value of the text node itself. Only elements, text, annotated with text value, return all other types of nodesnull
.textContent
Property returns a string that represents the current text node and all its descendant nodes. It will automatically ignore the current HTML tags inside a node, return all text content.parentNode
Property returns the parent node of the current node. For a node, its parent node may be only three types: document node (Document), the element node (element) and the document fragments node (documentfragment).nextSibling
The first sibling node of the current node returns the attribute (element, text, comment). As there are nonenull
.previousSibling
This property returns the first sibling node (element, text, comment) in front of the node. As there are nonenull
.firstChild
Property returns the first child node of the current node (element, text, comment). If the current node has no children, it is returnednull
.lastChild
Returns the last child attribute node (element, text, comment) of the current node. If the current node has no children, it is returnednull
.childNodes
Property returns an array object class (NodeList
set), including members of all the child nodes of the current node (element, text, comment).Since the
NodeList
object is a dynamic collection, once the child node changes will immediately be reflected in the results are returned.
When the above returns the text node if the current node in front of a space, the property will return a text node, content spaces .
1.2 Object methods
hasChildNodes
Methods used to indicate whether the current node has children.insertBefore
A method for inserting a node previously specified child node of the node (if the node to be inserted is currently existing DOM node, the node is removed from the original position, insert the new position). The first parameter is the method of the node to be insertednewNode
, the second parameter is a child nodereferenceNode
(if the second parameter isnull
, the new node is inserted at the last position inside the current node, i.e., becomes the last child node). The return value is the new node is insertednewNode
.removeChild
A method for removing the sub-node from the current node. The method parameter is a child node to be removed. The return value is the node removed.replaceChild
Methods used to a new node, replace a child of the current node. The method first parameternewChild
is used to replace the new node, the second parameteroldChild
is replaced to take the child node. The return value is the replacement node to gooldChild
.getRootNode()
Method returns the root node of the current document where the document.
Two, Document
document
Node object represents the entire document, each page has its own document
objects. This node has two child nodes: a first node is a document type ( <!doctype html>
); second top container tag of the HTML page <html>
, which latter constitutes the root node (root node) of the tree structure, which are nodes other HTML tags lower node. document
There are different ways objects can get:
- Normal web: using
document
orwindow.document
. iframe
Web: Usingiframe
nodecontentDocument
properties.
2.1 Object Properties
doctype
Owned property returns<DOCTYPE>
nodes.document.firstChild
Usually return this node.documentElement
Current property returns the root element node of the document.document.lastChild
Usually return this node.head
Attribute points to<head>
the node. This property is always there, if the page source code which is omitted, the browser will automatically be created.body
Property returns<body>
nodes. This property is always there, if the page source code which is omitted, the browser will automatically be created.location
Property is a native object provided by the browser, providing the relevant URL information and method of operation.cookie
Object attributes native provided by the browser, for operating the browser Cookie.
2.2 Object Methods
querySelectorAll
Returns the specified method of matching node selector element, a returnNodeList
instance. Return result is not a dynamic collection, it does not reflect real-time changes in element nodes. If multiple nodes satisfy the matching condition, a first matching node is returned. If a match is not found node is returnednull
. It does not support CSS selectors pseudo-element (such as:first-line
and:first-letter
) and pseudo-class selectors (such as:link
and:visited
) that can not be selected pseudo-elements and pseudo-classes.getElementById
Match the specified method returns theid
element node attributes, return anNodeList
instance of non-real time to reflect changes in the document.getElementsByName
Method returns the matchingname
element node attributes, return anNodeList
instance of non-real time to reflect changes in the document.getElementsByTagName
Method returns the matching tags node, returns anHTMLCollection
instance, can reflect the changes in the document in real time.getElementsByClassName
Method returns the name of the node class matches, returns anHTMLCollection
instance of the document may reflect changes in real time.createElement
A method for generating an element node, and returns the node.createAttribute
The method generates a new attribute node (Attr
instance), and returns to the node.createEvent
The method of generating an event object (Event
instance), the object may beelement.dispatchEvent
used a method, the specified triggering event. Parameter method is the type of event, such asUIEvents
,MouseEvents
,HTMLEvents
and so on. Event Processing reference the next chapter.
Three, attribute
3.1 Object Properties
Element.attributes
Property returns a similar array of dynamic objects, the members are all attributes of the element tag node objects, attributes the change will be reflected in real time on the node object. Property itself is an object ( Attr
the object). Therefore, similar to the above properties of the array are returned node object, rather than an attribute value. Node object has properties name
and value
attributes, attribute names and values should properties equivalent to nodeName
the properties and nodeValue
attributes.
HTML attribute names turn keys JS: When you turn JS attribute names, all lower case; if the attribute names include multiple words, the use of camel spelling. Some HTML attribute name is a reserved word in JavaScript must be renamed when it turns JS property, mainly the following two:
for
Property tohtmlFor
class
Property toclassName
Element.dataset
Property returns an object, the object can be read from this data-
property. As <div data-timestamp="1522907809292"></div>
, <div>
element has a custom data-timestamp
property for the element used to add a time stamp.
HTML attribute names turn JS keys: turn into
dataset
when the key name, followed by the conjunction line if followed by a lowercase letter, then the conjunction line is removed, the lowercase letters to uppercase letters, other characters unchanged. Converselydataset
when the keys turn into attribute names, all uppercase letters will be converted to the conjunction line + lowercase letters, and other characters unchanged. Asdataset.helloWorld
will turn intodata-hello-world
.
3.2 Object Methods
hasAttribute
Method returns a Boolean value indicating whether the current node contains the elements of the specified property.getAttribute
Method returns the current element node of the specified property. If the specified property does not exist, it returnsnull
.setAttribute
The method used for the current new property element. If the attribute of the same name already exists, edit existing properties is equivalent.removeAttribute
A method for removing the specified property.