Home >Web Front-end >JS Tutorial >What is the use of jQuery html() method

What is the use of jQuery html() method

青灯夜游
青灯夜游Original
2021-05-06 16:42:094840browse

html() method is used to return or set the content of the selected element: 1. If no parameters are set, return the current content of the selected element, syntax "$(selector).html()"; 2 . If parameters are set, the content of all matching elements will be overwritten, with the syntax "$(selector).html(content value)".

What is the use of jQuery html() method

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jQuery html() method

html() method returns or sets the content (inner HTML) of the selected element.

  • Return element content

When using this method to return a value, it returns the first matching element Content.

Syntax

$(selector).html()

Example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
		</script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					alert($("p").html());
				});
			});
		</script>
	</head>

	<body>
		<p>这是一个 <b>段落</b>。</p>
		<button>返回P标签的内容</button>

	</body>

</html>

What is the use of jQuery html() method

  • ##Set element content

When you use this method to set a value, it overwrites the content of all matching elements.

Syntax

$(selector).html(content)

content: Specifies the new content of the selected element, which can be omitted. This parameter can contain HTML tags.

Example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
		</script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("p").html("Hello <b>world!</b>");
				});
			});
		</script>
	</head>

	<body>

		<p>这是一个段落。</p>
		<p>这是另一个段落。</p>
		<button>修改所有P元素的内容</button>
	</body>

</html>

What is the use of jQuery html() method

Related video tutorial recommendation:

jQuery tutorial (video)

The above is the detailed content of What is the use of jQuery html() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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