How to change HTML content in JavaScript: Use the innerHTML attribute to modify it, such as [document.getElementById(id).innerHTML = new text].
The operating environment of this article: windows10 system, javascript 1.8.5&&html 5, thinkpad t480 computer.
The easiest way for JavaScript to modify html content is to use the innerHTML attribute. Let’s take a look at the syntax:
document.getElementById(id).innerHTML = new text
For example, if we want to modify the content of the
element:
<html> <body> <p id="p1">Hello World!</p> <script> document.getElementById("p1").innerHTML = "hello kitty!"; </script> </body> </html>
Explanation: The above HTML document contains the
element with id="p1". We use HTML DOM to get the element with id="p1". JavaScript changes the content of the element (innerHTML) to "Hello Kitty! "
Recommended learning: javascript video tutorial
The above is the detailed content of How javascript changes HTML content. For more information, please follow other related articles on the PHP Chinese website!