jQuery after()

时间:2020-02-23 14:46:04  来源:igfitidea点击:

jQuery after方法可用于添加HTML内容,DOM元素或者jQuery对象,以在匹配的元素集中的每个元素之后插入。
我们还可以在jQuery中的after方法中提供函数,该方法返回要在匹配的元素集之后添加的内容。

jQuery after

jQuery after方法与insertAfter()相同,不同之处在于语法上带有目标元素和要添加的内容的位置。

这是一个示例页面,其中我使用jQuery在HTML页面中动态添加新的段落和文本框。

<html>
<head>
<title>jQuery after() Examples</title>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script>
function addNewParagraph(){
	$("p:first").after("Dynamic Paragraph");
}

function addNewTextbox(){
	$("input:text:last").after("<br><input type='text' name='text2'>");
}
</script>
</head>
<body>
<p>First Paragraph</p>
<input type="button" onclick="addNewParagraph()" value="Add Paragraph">
<br>
<input type="text" name="text1">
<br>
<input type="button" onclick="addNewTextbox()" value="Add Textbox">
</body>
</html>