Example analysis of innerText and innerHTML attribute usage in javascript

  • 2020-06-12 08:25:10
  • OfStack

This article illustrates the use of the innerText and innerHTML attributes in javascript. Share to everybody for everybody reference. The specific analysis is as follows:

Almost all DOM elements have innerText and innertHTML attributes (case sensitive), respectively within the element tag
Text representation and HTML source code, both of which are readable and writable

innerHTML can also replace createElement, which is a simple, extensive, consequences-free creation

<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title> test (innerText and innerHTML)</title>
 <script type="text/javascript">
 function TestOutput() {
  var myLink = document.getElementById("lnk");
  alert('innerText-->' + myLink.innerText);
  // Get baidu website 
  alert('innerHTML-->' + myLink.innerHTML);
  // Get the best <font color="red"> The degree of </font> Web site 
 function EditInnerText() {
  var myLink = document.getElementById("lnk");
  myLink.innerText = " The website ";
 function EditInnerHTML() {
  var myLink = document.getElementById("lnk");
  myLink.innerHTML = "<font color='blue'> The website </font>";
 function dynamicButtonClick() {
  alert(' I created it dynamically ');
 function CreateButton() {
  var div = document.getElementById("divMain");
  div.innerHTML = "<input type='button' value=' Click on the I ' 
  onclick='dynamicButtonClick()' />";
<a href="http://www.baidu.com" id="lnk">
 best <font color="red"> The degree of </font> Web site </a>
<input type ="button" value=" test " onclick="TestOutput()"/>
<input type="button"" value=" Modify the InnerText" onclick="EditInnerText()"/>
<input type="button"" value=" Modify the InnerHTML" onclick="EditInnerHTML()"/>
<div id="divMain"></div>
<input type="button" value=" Dynamic add button " onclick="CreateButton()"/>

I hope this article has been helpful for your javascript programming.

Related articles: