JavaScript Node Properties

Source: Internet
Author: User
Tags tag name

Node properties

In the Document Object Model (DOM), each node is an object. The DOM node has three important properties:

1. NodeName: Name of the node

2. NodeValue: The value of the node

3. NodeType: Type of node

One, NodeName property: The name of the node, is read-only.

1. The nodeName of the element node is the same as the label name
2. The nodeName of the attribute node is the name of the property
3. The nodeName of a text node is always #text
4. The nodeName of the document node is always #document

Second, NodeValue property: The value of the node

1. The nodevalue of the element node is undefined or null
2. Text node NodeValue is the text itself
3. The nodevalue of the attribute node is the value of the property

nodeType attribute: The type of the node, which is read-only. Here are some common types of nodes:

Element type node type
Element 1
Property 2
Text 3
Note 8
Document 9

Example: Get all the LI tags and output the name of the corresponding node, the value of the node, and the type of the node.

<! DOCTYPE html>


<meta http-equiv=" Content-type "content=" text/html; Charset=utf-8
<title> node properties </title>

<body>
<ul>
<li>javascript</li>
<li>html/css</li>
<li>jQuery</li>
</ul>
<script Type= "Text/javascript";

var node=document.getelementsbytagname ("Li"); //Get Li tags

for (Var i=0;i<node.length;i++)

{
document.write ("li[" + (i+1) + "]:" + "<br/>" + "node Name:" +node[i].nodename+ "<br/>");
document.write ("Node value:" +node[i].nodevalue+ "<br/>");
document.write ("Node type:" +node[i].nodetype+ "<br/><br/>");
}//Output All LI element node properties

</script>
</body>

Output Result:

    • Javascript
    • Html/css
    • Jquery

LI[1]:
Node Name: LI
Node value: null
Node Type: 1

LI[2]:
Node Name: LI
Node value: null
Node Type: 1

LI[3]:
Node Name: LI
Node value: null
Node Type: 1

Note: The tag name of the XML element contained in NodeName is always capitalized!!!

JavaScript Node Properties

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.