JavaScript innerText



 JavaScript innerText is a bit similar to innerHTML . The difference is that innerText will filter out HTML tags in the string . For example, through innerText to obtain the value of a specific element in a web page <p>string</p>, the result is only "string", and HTML tags <p> and </p> will be filtered out, and the examples have detailed presentation methods.


JavaScript innerText basic syntax
HTMLDOMObject.innerText
The basic syntax is similar to innerHTML , but innerText is only applicable to IE browser.

JavaScript innerText example
<script type="text/javascript">
function FindValue(){
    var ShowString=document.getElementById("TestBox").innerText;
    document.getElementById("ShowBox").innerText=ShowString;
}
</script>
<div id="TestBox" onclick="FindValue()">
    <font color='blue'>This is test string.</font>
</div>
<div id="ShowBox"></div>
The above example output
This is test string.
This is test string.
The example prepares a blue string in advance and puts it in the DIV block whose id is TestBox , and monitors and triggers the FundValue function through the onclick event, so the operation of the example is to click the blue example string, The black string below will automatically jump out. The text content is the same, but the color of the text is different. The main reason is that innerText filters out the HTML font tags. There are only two lines of code in the FindValue function. The first line is to get all the characters in the DIV block through the getElementById method of the DOM . Of course, it also includes the font tag in the example . Since innerText is used, the obtained characters are The HTML tags will be filtered out before being stored in the variable ShowString. The second line code is to write the variable value obtained in the first line into the DIV block with the id ShowBox through innerText. Please note this The example can only be used in IE browser. 

Post a Comment

0 Comments