The innerHTML attribute allows you to change the content of the html element. You can set or return the HTML between the start and end tags of the table row. This article will introduce you to the usage of innerHTML. Let’s go See the specific content.
How to write innerHTML
For element, set the element name specified by innerHTML.
element.innerHTML;
Assign the html code of the specified element to the variable
By writing as follows, we assign the html code of the specified element to the variable sample1
var sample1 = element.innerHTML;
Replace the value of the variable with the specified element
Writing as follows, you can display the content of the variable sample2 on the element element after deleting the content of the element element.
element.innerHTML = sample2;
Clear the specified element
You can clear the content of the element element by specifying "".
element.innerHTML = “”;
Let’s look at the specific examples of innerHTML usage
The basic code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div id="txt"> <div id="txt1">文字内容1 </div> <span id="txt2">文字内容2 </span> </div> <p>---------------------</p> <div id="innerHTMLtxt"></div> </body> </html>
The running results are as follows
Let’s look at the results of using the innerHTML attribute
<script type="text/javascript"> innerHTMLtxt.innerHTML="php中文网!"; </script>
<script> txt.innerHTML=""; </script>
The above is the detailed content of How to use innerHTML in JavaScript. For more information, please follow other related articles on the PHP Chinese website!