Get element text

Source: Internet
Author: User

I remember John resig said that the class library is used to block the differences between browsers, but his gettext function obviously cannot do this. Why don't I remind him? My English is really bad. I can still read it, but I want to write a few serious words in English. It is estimated that the sentences are all sick sentences ...... If no error is reported in IE, refresh the current page.

<Textarea id = "runcode1" style = "width: 75%" rows = "10"> <br/> <! Doctype HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> <br/> <SCRIPT src =" http://common.cnblogs.com/script/jquery.js "type =" text/JavaScript "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "> </P> <p> $ (function () {<br/> alert ($ ("p "). text (); <br/>}); </P> <p> </SCRIPT> <br/> <title> jquery. text test by situ zhengmei </title> <br/> </pead> <br/> <body> </P> <p> <P> situ zhengmei <br/> <! -- This is the comment node // --> <br/> <a href = "javascript: void (0) "href =" http://www.cnblogs.com/rubylouvre/ "> Ruby's Louvre </a> <br/> </P> <br/> <p> situ zhengmei </P> <p> </body> <br/> </ptml> <br/> </textarea>

RunCode

There are obviously fewer blank lines and blank lines in IE, because IE will automatically delete the blank node. Therefore, to keep all browsers unchanged, only the blank nodes should be removed. Also, I don't understand why jquery will concatenate the text of all matching elements into one. Why don't we return a string array, so we need to break it down one by one ......

The following is my solution:

 
// @ Author: zhongqincheng // gets the text of a node. If this node is an element node, It retrieves all the text of its childnodes, // ignore all blank nodes to make the results consistent in all browsers. Therefore, it is non-element innertext or textcontent var gettext = function (nodes) {var result = "", node; for (VAR I = 0, n = nodes. length; I <n; I ++) {node = nodes [I]; // if it is a text node or CDATA node if (node. nodetype = 3 | node. nodetype = 4) {If (! /^ \ S + $ /. test (node. nodevalue) // ignore the blank node and linefeed result + = node. nodevalue; // if it is an element node} else if (node. nodetype = 1) {result + = gettext (node. childnodes) ;}} return result ;}

check the effect:

<Br/> <! Doctype HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "> </P> <p> <SCRIPT type =" text/JavaScript "> </P> <p> // @ Author: situ zhengmei (zhongqincheng) <br/> // obtain the text of a node. If the node is an element node, retrieve all the text of its childnodes, <br/> // ignore all blank nodes to make the results consistent in all browsers, so it is not an innertext or textcontent element <br/> var gettext = function (nodes) {<br/> var result = "", node; <br/> for (VAR I = 0, n = nodes. length; I <N; I ++) {<br/> node = nodes [I]; <br/> // if it is a text node or CDATA node <br/> If (node. nodetype = 3 | node. nodetype = 4) {<br/> If (! /^ \ S + $ /. test (node. nodevalue) <br/> result + = node. nodevalue; <br/> // if it is an element node <br/>} else if (node. nodetype = 1) {<br/> result + = gettext (node. childnodes); <br/>}< br/> return result; <br/>}< br/> window. onload = function () {<br/> var P = document. getelementsbytagname ("p"); <br/> alert (gettext (p )) <br/>}</P> <p> </SCRIPT> <br/> <title> jquery. text test by situ zhengmei </title> <br/> </pead> <br /> <Body> </P> <p> situ zhengmei <br/> <! -- This is the comment node // --> <br/> <a href = "javascript: void (0) "href =" http://www.cnblogs.com/rubylouvre/ "> Ruby's Louvre </a> <br/> </P> <br/> <p> situ zhengmei </P> <p> </body> <br/> </ptml> <br/>

run the Code

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.