jQuery的insertAfter()方法怎么用?

insertAfter()是jQuery的一个内置的方法,用于在一个指定的元素之后插入一些HTML内容。下面本篇文章就来给大家介绍一下insertAfter()的用法,希望对大家有所帮助。

insertAfter()方法会在每次出现指定元素后插入HTML内容。

语法:

$(content).insertAfter(target)

content:表示需要插入的HTML内容。

target:表示在插入内容前需要查找到的指定目标。

注:insertAfter()方法不返回任何值。

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
		<script>
			$(document).ready(function() {
				$(".demo").click(function() {
					// insertBefore 
					$("<div style='color: red;'>测试文本!</div>").insertAfter("p");
				});
			});
		</script>
	</head>
	<body style="text-align: center;">
		<div class="box">
			<p>学习jQuery</p>
			<p>学习javascript</p>
			<button class="demo">单击此处</button>
		</div>
	</body>
</html>

效果图:

1.gif

以上就是jQuery的insertAfter()方法怎么用?的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » jQuery 答疑