DOM node operation with native JS

Source: Internet
Author: User

The DOM (Document Object model/) is an API for HTML and XML documents.
DOM node tree: spaces, carriage returns, labels, notes, text, doctype, labels, and so on are all part of the DOM node in the document.

The way to manipulate DOM nodes is to create, add (insert), remove, replace, find (get), and clone DOM nodes.

Create text node:var newtext = document.createtextnode (' text node ');
Create tag node:var newNode = document.createelement (' div ');

Add (insert) child node to end: parent node. appendchild (child node);
Add (insert) child nodes to a node before: parent node. InsertBefore (child node, a node);

Remove child nodes: parent node. RemoveChild (child node);

Replace node: parent node. ReplaceChild (replaced node, node before replacement);

Find (get) node:
find (GET) All child nodes: parent node. childNodes
find (GET) all child nodes that are label types: parent node. Children
find (Get) Next sibling node: a node. nextSibling
find (get) the previous sibling node: a node. previoussibling
Find (Get) parent node: child node. parentnode
find (GET) first child node: parent node. firstchild
find (GET) Last child node: parent node. LastChild

You can also use document.get ... method to get, detailed see: JS get Tag method and compatible processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.