jquery怎么替换div内容

jquery怎么替换div内容,第1张

jquery怎么替换div内容

在jquery中,可以使用html()方法来替换div元素的内容,该方法可以设置并覆盖所有匹配元素的内容,语法为“$("div").html("新的div内容")”。

本教程 *** 作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。

在jquery中,可以使用html()方法来替换div元素的内容。

html() 方法返回或设置被选元素的内容 (inner HTML)。

当给该方法传入一个参数,设置一个值时,它会覆盖所有匹配元素的内容。

语法:

$(selector).html(content)
//或
$(selector).html(function(index,oldcontent))
参数描述content可选。规定被选元素的新内容。该参数可包含 HTML 标签。参数描述function(index,oldcontent)

规定一个返回被选元素的新内容的函数。

  • index - 可选。接收选择器的 index 位置。
  • oldcontent - 可选。接收选择器的当前内容。

示例:

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

【推荐学习:jQuery视频教程、web前端视频】

以上就是jquery怎么替换div内容的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/698998.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-22
下一篇2022-04-22

发表评论

登录后才能评论

评论列表(0条)

    保存