Home > Web Front-end > Front-end Q&A > There are several ways to get html page value in javascript

There are several ways to get html page value in javascript

青灯夜游
Release: 2022-03-28 19:17:39
Original
6167 people have browsed it

There are three ways to get the HTML page value in JavaScript: 1. Use the "html element object.innerHTML" statement to return the content of the element; 2. Use the "html element object.innerText" statement; 3. Use " input element object.value" statement can return the content in the input box.

There are several ways to get html page value in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are three ways to get the html page value in javascript

  • Use the innerHTML attribute

  • Use innerText Attribute

  • Use value attribute

innerHTML attribute

innerHTML attribute sets or returns the start and end of the element HTML between closing tags.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h1>这是一个大标题</h1>
		<p id="p">这是一段<b>文字</b>。</p>
		<script>
			var nr = document.getElementById("p").innerHTML;
			alert(nr);
		</script>
	</body>
</html>
Copy after login

There are several ways to get html page value in javascript

Use the innerText attribute

The innerHTML attribute sets or returns the text content between the opening and closing tags of the element.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h1>这是一个大标题</h1>
		
		<p id="p">这是一段<b>文字</b>。</p>
		<script>
			var nr = document.getElementById("p").innerText;
			alert(nr);
		</script>
	</body>
</html>
Copy after login

There are several ways to get html page value in javascript

Use the value attribute

The value attribute sets or returns the value of the input element

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	</head>
	<body>
		<h1>这是一个大标题</h1>
		
		<p id="p">这是一段<b>文字</b>。</p>
		<input type="text" id="in" value="hello" />
		<script>
			var nr = document.getElementById("in").value;
			alert(nr);
		</script>
	</body>
</html>
Copy after login

There are several ways to get html page value in javascript

【Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of There are several ways to get html page value in 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