Home >Web Front-end >JS Tutorial >How to use Dom attributes
This time I will bring you how to use Dom attributes, and what are the precautions when using Dom attributes. The following is a practical case, let's take a look.
1. Grammar standard of ECMAScript js
2.DOM Document object Model Document Object Model provides methods that allow js to operate html tags
3.BOM Browser Object Model Browser Object Model provides methods that allow js to operate browsers
Note: 1. The biggest boss in js is window, document is just an object under window
document.documentElement This thing can be obtained from html
document(在文档里,document是老大) | html / \ / \ / \ body head / / | \ / / | \ / / | \ / | \ meta title style... / | \ / | \ p p ul... / / a
js ---> DOM --> html
js operates html tags through DOM
childNodes returns all child node objects under the current object, and will return text nodes
Note: Only elements will be returned under ie8 Node
nodeType returns the node type, element node returns 1, text node returns 3 CommentsNode returns 8
children Returns all child element nodes under the object, and there are no compatibility issues
firstChild returns the first child node, which behaves the same as childNodes in IE8
lastChild returns the last child node, the same as above
firstElementChild returns the last element node, not compatible with IE8
nextSibling The next sibling node in mainstream browsers may get nodes other than element nodes. In IE8, only element nodes will be returned. If not, Return null
previousSibling The previous sibling node is the same as above
nextElementSibling If there is no next sibling element node, return null. Incompatible with IE678
previousElementSibling Same as above
##parentNode * Returns the parent node without compatibility
offsetParent Returns the positioning parent. If neither is found, it will finally return to the body. There is no compatibility issue
Capital letters
3. Some methods of DOM createElement('p') This is the tag To create a new element node, you need to accept a parameter, and the parameter is the label that needs to be created. CreateTextNode() Create a new text node createComment() Create a new comment nodeNode operationAdd element node
1. Parent.appendChild(child node) Add the child node to the parent node after all the child element nodes of the parentAppend a node
2. Parent.insertBefore(child node, specified child node) Add to the front of the specified nodeDeep copy: Copy the in-line information of some tags related to this tag object, and the custom attributes in js will not be copied.
These are my notes on dom learning. If it can help you, I will be very happy.
# I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
##How to implement two-way data binding in WeChat applet
How to use JavaScript strings
The above is the detailed content of How to use Dom attributes. For more information, please follow other related articles on the PHP Chinese website!