Home >Web Front-end >JS Tutorial >How to click a button to modify text with JavaScript
How to modify text by clicking a button in JavaScript: 1. Create a button element, use the onclick attribute to bind the click event, and set the event processing function; 2. In the event processing function, use "element object.innerHTML= The "modified text content";" statement can modify the text content of the element.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript clicks the button to modify the text
Implementation idea:
Create a button element, use The onclick attribute binds the click event and sets the event processing function;
In the event processing function, use the innerHTML attribute to modify the text content of the element.
Implementation code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="con">默认文本内容</p> <button onclick="myFunction()">点击按钮修改文本内容</button> <script> function myFunction() { var p=document.getElementById("con"); p.innerHTML="修改后的文本内容"; } </script> </body> </html>
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to click a button to modify text with JavaScript. For more information, please follow other related articles on the PHP Chinese website!