Home > Web Front-end > JS Tutorial > body text

How to click a button to modify text with JavaScript

青灯夜游
Release: 2022-01-19 10:45:41
Original
9959 people have browsed it

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.

How to click a button to modify text with JavaScript

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>
Copy after login

How to click a button to modify text with JavaScript

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template