JavaScript gets the general function of element text content _javascript technique

Source: Internet
Author: User
<textarea id="runcode10726"><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> a universal function to get element text content </title> </pead> <body> <p id=" P1 "> People's Republic of China <b>1</b> <span>2</span></p> </body> <script language= "JavaScript" ><!- -///A universal function that gets the content of the element text (e) {var t= ""; E=e.childnodes | | e;//If the element is passed in, it continues to traverse its child elements, otherwise it is assumed to be an array for (Var i=0;i<e.length;i++) {//if not an element, to recover its text value;//Otherwise, recursively iterate through the child nodes of all elements; t + = E[i].nodet Ype!= 1? E[i].nodevalue:text (E[i].childnodes); } return t; var P1=document.getelementbyid ("P1"); Alert (Text (p1)); Return to the People's Republic of China 1 2 alert (text (p1.lastchild)); Return 2 alert (text (p1.firstChild.nextSibling));//return 1//--></script> </ptml></textarea>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Make a record of several easily-used properties in the HTML DOM:

NodeName, NodeValue, and NodeType contain information about the nodes.
The NodeName property contains the name of a node.

The nodename of the element node is the label name
The nodename of the attribute node is the property name
The nodename of the text node is always #text
The nodename of the document node is always #document
Note: The label name of the XML element contained in NodeName is always uppercase

NodeValue
For text nodes, the NodeValue property contains text.

For an attribute node, the NodeValue property contains the property value.

The NodeValue property is not available for document nodes and ELEMENT nodes.

NodeType
The NodeType property can return the type of the node.

The most important node types are:

Element type = = "Node type
Elemental element = = 1
Attribute attr = = "2
Text = = "3
Note comments = = "8
Documents Document = = 9

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.