Home > Article > Web Front-end > How to change text content in javascript
Javascript method to change text content: First use the "document.getElementById("element id value")" statement to obtain the element object; then use the innerHTML attribute to change the text content, the syntax format is "element object.innerHTML= text;".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript changes the text content of the html web page
<body> <p id="demo">JavaScript能改变html文本内容</p> <script type="text/javascript"> function myFunction() { x=document.getElementById("demo"); // 找到元素 x.innerHTML="Hello JavaScript!"; // 改变内容 } </script> <button type="button" οnclick="myFunction()">点击这里</button> </body>
Code screenshot:
Demo screenshot:
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to change text content in javascript. For more information, please follow other related articles on the PHP Chinese website!