The examples in this article describe the usage of innerText and innerHTML attributes in javascript. Share it with everyone for your reference. The specific analysis is as follows:
Almost all DOM elements have innerText and innertHTML attributes (note the case), which are within the element tag
Text representation and HTML source code, these two attributes are readable and writable
innerHTML can also replace createElement, which is a simple, extensive and self-responsible creation
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试(innerText和innerHTML)</title> <script type="text/javascript"> function TestOutput() { var myLink = document.getElementById("lnk"); alert('innerText-->' + myLink.innerText); //得到百度网站 alert('innerHTML-->' + myLink.innerHTML); //得到百<font color="red">度</font>网站 } function EditInnerText() { var myLink = document.getElementById("lnk"); myLink.innerText = "凤凰网"; } function EditInnerHTML() { var myLink = document.getElementById("lnk"); myLink.innerHTML = "<font color='blue'>凤凰网</font>"; } function dynamicButtonClick() { alert('我是动态创建的'); } function CreateButton() { var div = document.getElementById("divMain"); div.innerHTML = "<input type='button' value='单击我' onclick='dynamicButtonClick()' />"; } </script> </head> <body> <a href="http://www.baidu.com" id="lnk"> 百<font color="red">度</font>网站</a> <input type ="button" value="测试" onclick="TestOutput()"/> <input type="button"" value="修改InnerText" onclick="EditInnerText()"/> <input type="button"" value="修改InnerHTML" onclick="EditInnerHTML()"/> <div id="divMain"></div> <input type="button" value="动态添加按钮" onclick="CreateButton()"/> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.