jquery怎么替换div内容-LMLPHP

本教程操作环境: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怎么替换div内容-LMLPHP

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

以上就是jquery怎么替换div内容的详细内容,更多请关注Work网其它相关文章!

09-17 02:05