Home >Web Front-end >Front-end Q&A >How to replace div content with jquery

How to replace div content with jquery

青灯夜游
青灯夜游Original
2022-03-11 14:00:304851browse

In jquery, you can use the html() method to replace the content of the div element. This method can set and cover the content of all matching elements. The syntax is "$("div").html("new div content ")".

How to replace div content with jquery

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

In jquery, you can use the html() method to replace the content of the div element.

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

When a parameter is passed to this method and a value is set, it will overwrite the content of all matching elements.

Syntax:

$(selector).html(content)
//或
$(selector).html(function(index,oldcontent))
Parameters Description
content Optional. Specifies the new content of the selected element. This parameter can contain HTML tags.
Parameters Description
function(index ,oldcontent)

Specifies a function that returns the new content of the selected element.

  • index - Optional. Receives the index position of the selector.
  • oldcontent - Optional. Receives the current contents of the selector.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					 $("div").html("Hello <b>world!</b>");
				});
			});
		</script>
	</head>
	<body>

		<div>这是一段文字。</div>
		<div>这是另一段文字。</div><br>
		<button>改变 div 元素的内容</button>
	</body>
</html>

How to replace div content with jquery

[Recommended learning: jQuery Video tutorialweb front-end video

The above is the detailed content of How to replace div content with jquery. 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