JavaScript gets div's content essence _javascript tips

Source: Internet
Author: User
Principle: Using InnerText or InnerHTML
Copy Code code as follows:

<script language= "JavaScript" >
var stock_code = Stockcode.innertext;
var stock_code = stockcode.innerhtml;
</script>
<div id= "Stockcode" style= "Display:none" >
Test
</div>


InnerText and innerHTML are two non-dom standard methods.
The difference is as shown in the figure:
(The figure should be innertext)


In IE, innertext and inner HTML two methods can operate correctly

But the innertext in FF is not available, but there is an alternative: textcontent

IE:oDiv.innerText = astring; odiv.innerhtml = astring;
FF:oDiv.textContent = astring; odiv.innerhtml = astring;

One of the authors of Ajax in action Eric uses a regular expression to implement a compatible method, which is more interesting
Hope this helps
A Little Smirk
One day a secretary are leaving on her lunch break, and she notices her boss standing in front of a shredder with a clueles s look in his face. The Secretary walks up to him and asks if him needs help.
"yes!" he says looking and sounding relieved, "This is very important."
Glad to help, she turns the shredder on and inserts the paper. Then her boss says, "I only need one copy."
Create function like innertext
As you may have figured out innertext be IE only. That means which browsers like Mozilla, Firefox, and Netscape would return undefined. If you don't know what innertext does, it strips out all of the "tags so" and "only" the text.
For example, if a div contains the HTML <span id= ' span1 ' >eric</span> InnerHTML would return <span ' span 1 ' >Eric</span> while innertext'll return to Eric.
The InnerHTML act the same we need to use some regular the expressions replace () method.
Now "basic pattern" we need to match is OR or
Now the regular expression we are need to use is/<\/? [^>]+>/gi
If you don't know regular expressions is a quick explanation:
/-Starts the regular expression
<-Match the less than sign
\/-Escape The character/so it can be matched (without the ' you would being saying it is the ' end of the ' the ' reg exp.)
? -Match the/character 0 or 1 times
[^>]-Match any character but greater than sign
+-Match [^>] one or more times
>-Match greater than sign
/-End the regular expression
Gi-tells regular expression to match global and ignore the case
So is the function to replace the text would look like:
Copy Code code as follows:

<script type= "Text/javascript" >
var regExp =/<\/? [^>]+>/gi;
function Replacetags (XSTR) {
Xstr = Xstr.replace (RegExp, "");
return XSTR;
}
</script>

All are need to do are pass it a string and it returns the string stripped of the tags.
An example are shown below to grab the text from a div without the tags.
<ptml> <pead> <script type= "Text/javascript" > var regExp =/<\/? [^>]+>/gi; function Replacetags (xstr) {xstr = Xstr.replace (RegExp, ""); return XSTR; } </script> </pead> <body> <div id= "Test" > <span id= "span1" >test <u><b>test </b></u> Test wow!</span> </div> <script type= "Text/javascript" > var xcontent = document . getElementById ("Test"). InnerHTML; var fixedcontent = replacetags (xcontent); alert (fixedcontent); </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.