jQuery—添加DOM节点之append方法和appendTo()

appendTo兼容性更好

append(content|fn) : 向每个匹配元素内部的 末尾 处插入内容;

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<p id="content">I love </p>
		<p id="content1">I hate </p>
		<script>
			$("#content").append("<span>you.</span>");
		</script>
	</body>
 
	</body>
</html>

页面显示为:

I love you.

I hate

appendTo(content) : 将每个匹配的元素追加到指定的元素中的 内部 的末尾;

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery-3.3.1.js"></script>
	</head>
	<body>
		<p id="content">I love </p>
		<span id="letters">you.</span>
		<script>
			$("#letters").appendTo("#content");
		</script>
 
	</body>
 
	</body>
</html>

您可能还喜欢...

发表回复