<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