"Javascript" Dom node

Source: Internet
Author: User

  Everything in the HTML document is a node !

The entire document is a document node;

Note is a comment node;

Each HTML element is an element node;

The text content within the element is a text node;

Each property of a hyphen element is an attribute node.

It feels familiar to see these things, right, just like everything in JS is an object, everything in the HTML document is a node.

We can use a variety of access nodes to the method of any one of the nodes, such as pruning and checking operations. To achieve a variety of operations on the entire page.

  Increase :

If you want to add an element to HTML, you need to first create an element (the element node) and then append it to the existing element.

To create an element node NewNode:

var newNode = document.createelement (String tagName)

To create a text node within an element:

var textnode = document.createTextNode (String data)

Add the text node Textnode to the newly created element node NewNode:

Newnode.appendchild (Textnode)

Add the newly created element node NewNode to the end of the existing element section OldNode point:

Oldnode.appendchild (NewNode)

Or add the newly created element node newnode to a child node of an existing element node OldNode before Childnode:

Oldnode.insertbefore (Newnode,childnode)

Example:

1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<script src= "Jquery.js" ></script>6<script type= "Text/javascript" >7Window.onload =function(){8                 varpp = document.createelement ("li");9                 varHH1 = document.createelement ("H1");Ten                 varTtext = document.createTextNode ("This is the newly added element node!")); One Pp.appendchild (HH1); A Hh1.appendchild (ttext); -                 varull = document.getElementById ("ul"); - ull.appendchild (PP); the             } -</script> -<style type= "Text/css" > - ul { + border:1px solid; - margin:100px Auto; + width:400px; Alist-style-Type:none; at             } -</style> -<title>JS</title> - -<body> -<ul id= "ul" > in<li> -<li> to</ul> +</body> -

Effect:

  

  

  By deleting:

After finding the node that will be deleted Removenode and the parent node of the node parentnode

Parentnode.removechild (RemoveNode)

Example:

1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<script src= "Jquery.js" ></script>6<script type= "Text/javascript" >7Window.onload =function(){8                 varpp = document.createelement ("li");9                 varHH1 = document.createelement ("H1");Ten                 varTtext = document.createTextNode ("This is the newly added element node!")); One Pp.appendchild (HH1); A Hh1.appendchild (ttext); -                 varull = document.getElementById ("ul"); - ull.appendchild (PP); the                  -                 varLi1 = document.getElementById ("Li1"); - Ull.removechild (LI1); -             } +</script> -<style type= "Text/css" > + ul { A border:1px solid; at margin:100px Auto; - width:400px; -list-style-Type:none; -             } -</style> -<title>JS</title> in -<body> to<ul id= "ul" > +<li id= "Li1" > -<li> the</ul> *</body> $

Effect:

    

  Change:
Find the element node that needs to be changed, and then change its text, style, and so on

Change its style:

Node.style.color= "Red"

Change its text:

Node.innerhtml= "string"

Instance:

1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<script src= "Jquery.js" ></script>6<script type= "Text/javascript" >7Window.onload =function(){8                 varpp = document.createelement ("li");9                 varHH1 = document.createelement ("H1");Ten                 varTtext = document.createTextNode ("This is the newly added element node!")); One Pp.appendchild (HH1); A Hh1.appendchild (ttext); -                 varull = document.getElementById ("ul"); - ull.appendchild (PP); the                  -                 varLi1 = document.getElementById ("Li1"); - Ull.removechild (LI1); -                  +                 varLi2 = document.getElementById ("Li2"); -Li2.style.color= "Red"; +Li2.innerhtml= "changed the color and text of the second line! " A             } at</script> -<style type= "Text/css" > - ul { - border:1px solid; - margin:100px Auto; - width:400px; inlist-style-Type:none; -             } to</style> +<title>JS</title> - the<body> *<ul id= "ul" > $<li id= "Li1" >Panax Notoginseng<li id= "Li2" > -</ul> the</body> +

Effect:

  

  Check:

In addition to using getElementById (), getElementsByTagName (), and Getelementclassname () to get a specific element node, we can also use the getattribute (String attribute) to get the value of a property of a particular element node and change the value of the property by SetAttribute (Attrname,attrvalue).

Instance:

1<script type= "Text/javascript" >2Window.onload =function(){3                 varpp = document.createelement ("li");4                 varHH1 = document.createelement ("H1");5                 varTtext = document.createTextNode ("This is the newly added element node!"));6 Pp.appendchild (HH1);7 Hh1.appendchild (ttext);8                 varull = document.getElementById ("ul");9 ull.appendchild (PP);Ten                  One                 varLi1 = document.getElementById ("Li1"); A Ull.removechild (LI1); -                  -                 varLi2 = document.getElementById ("Li2"); theLi2.style.color= "Red"; -Li2.innerhtml= "changed the color and text of the second line! " -                  -Alert (Li2.getattribute ("style")));19            } -</script>

Effect:

  

In addition, we can get more information about a node through the nodename, NodeType, and NodeValue of a node:

  The NodeName attribute specifies the name of the node: 

      • NodeName is read-only
      • The nodeName of the element node is the same as the label name
      • The nodeName of the attribute node is the same as the property name
      • The nodeName of a text node is always #text
      • The nodeName of the document node is always #document

Note: NodeName always contains the uppercase label name of an HTML element.

  The NodeValue property specifies the value of the node:

      • The nodevalue of the element node is undefined or null
      • The nodevalue of a text node is the text itself
      • The nodevalue of the attribute node is the property value

  The NodeValue property specifies the value of the node:

      • The nodevalue of the element node is undefined or null
      • The nodevalue of a text node is the text itself
      • The nodevalue of the attribute node is the property value

Javascript DOM node

Related Article

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.