Simple parsing of nodename, nodevalue, and nodetype

Source: Internet
Author: User
ArticleDirectory
    • Nodename, nodevalue, and nodetype contain information about nodes.
Record several common attributes in html dom:
Nodename, nodevalue, and nodetype contain information about nodes.

The nodename attribute contains the name of a node.

    • The nodename of the element node is the label name.
    • The nodename of the attribute node is the attribute name.
    • The nodename of the text node is always # text
    • The nodename of the document node is always # document

Note:The tag name of the XML element contained by nodename is always capitalized.

Nodevalue

For text nodes, the nodevalue attribute contains text.

For an attribute node, the nodevalue attribute contains the attribute value.

The nodevalue attribute is unavailable for document nodes and element nodes.

Nodetype

The nodetype attribute returns the node type.

The most important node types are:

Element type Node Type
Element 1
Attribute ATTR 2
Text text 3
Comment comments 8
Document 9

HTML file: <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head >
< Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" />
< Title > Dom Standard </ Title >
< Script Type = "Text/JavaScript" SRC = "Test. js" > < / JS>
</Head>
< Body >
< H1 ID = " H1 " > An HTML document < / H1>
< P ID = " P1 " > This is < I > W3C HTML dom < / I> document. < / P >
< P > < Input ID = " Btndemo1 " Type = " Button " Value = " Obtain the H1 Element Node Value " > < / P>
< P > < Input ID = " Btndemo2 " Type = " Button " Value = " Get H1 Element Node text " > < / P>
< P > < Input ID = " Btndemo3 " Type = " Button " Value = " Document Element Node text " > < / P>
< P > < Input type = " Button " ALT = " This is a demo button. " Title = " Title of the demo button prompt " Name = " Btnshowattr " ID = " Btnshowattr " Value = " Button node demonstration " / > < / P >
< / Body>
< / HTML>

JS:Function showelement (){
VaR element = Document. getelementbyid ("h1"); // H1 is<H1>Tag
Alert ('nodetype: '+ element. nodetype); // nodetype = 1
Alert ('nodename: '+ element. nodename );
Alert ('nodevalue: '+ element. nodevalue); // null
Alert ('element: '+ element );
}

Function showtext (){
VaR element = Document. getelementbyid ("h1 ");
VaR text = element. childnodes [0];
Alert ('nodetype: '+ text. nodetype); // nodetype = 3
Alert ('nodevalue: '+ text. nodevalue); // The nodevalue of the text node is the text content.
Text. nodevalue = text. nodevalue + "ABC"; // Add, modify, delete, and so on.
Alert ('nodename: '+ text. nodename );
Alert (text. data); // data is the same content, which can also be added, deleted, or modified.
}

Function showdocument (){
Alert ('nodetype: '+ document. nodetype); // 9
Alert ('nodename: '+ document. nodename );
Alert (document );
}

Function showattr (){
VaR btnshowattr = Document. getelementbyid ("btnshowattr"); // The demo button has many attributes.
VaR attrs = btnshowattr. attributes;
For (VAR I = 0; I<Attrs. Length; I ++ ){
VaR ATTR= Attrs [I];
Alert ('nodetype: '+ ATTR. nodetype); // attribute's nodetype= 2
Alert ('attr: '+ ATTR );
Alert ('attr. Name: '+ ATTR. Name +'= '+ ATTR. value );

}

}

function demo () {
var btndemo1 = document. getelementbyid ("btndemo1");
btndemo1.onclick = showelement; // select node nodetype value from button 1
var btndemo2 = document. getelementbyid ("btndemo2");
btndemo2.onclick = showtext;
var btndemo3 = document. getelementbyid ("btndemo3");
btndemo3.onclick = showdocument;
var btnshowattr = document. getelementbyid ("btnshowattr");
btnshowattr. onclick =showattr;

}< br> window. onload = demo;

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.