The difference between JavaScript innerHTML and innerText
JavaScript’s innerHTML and innerText look similar, but in fact there is a big difference. Most designers should be familiar with innerHTML. It is the syntax used to obtain HTML elements or write strings into HTML web pages, and innerHTML is W3C stipulates the standard writing method, and innerText can not only be used to obtain HTML elements, but also removes the HTML tags of the elements. However, innerText is not a standard writing method prescribed by the W3C and is only applicable to IE browsers. Very important.
JavaScript innerHTML and innerText basic syntax
JavaScript innerHTML and innerText examples
function ChangeFontColor(){
var OriginalFont_1=document.getElementById("StringFont_1").innerHTML;
var OriginalFont_2=document.getElementById("StringFont_2").innerText;
document.getElementById("StringFont_1" ).innerHTML='<font color="blue">'+OriginalFont_1+'</font>';
document.getElementById("StringFont_2").innerHTML=OriginalFont_2;
}
</script>
<p id="StringFont_1">through innerHTML turns text into blue</p>
<p id="StringFont_2"><font color='blue'>Through innerText turns text into black</font></p>
<input type="button" value ="Change text color" onclick="ChangeFontColor()">
Turn text to blue through innerHTML
Turn text to black through innerText
Post a Comment
0 Comments