jQuery insertAfter()方法
insertAfter()方法是jQuery内置的方法,用于在一个指定的元素后面插入一些HTML内容。该HTML内容将被插入到指定元素的每一次出现之后。
语法:
$(content).insertAfter(target)
这里的 “内容 “是指要插入到指定目标之后的HTML内容。
参数:它需要一个参数 “目标”,它是要插入内容的目标。
返回值:它不返回任何值。
例子1:在这个例子中,内容将在点击了 <div>
元素。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
(document).ready(function () {
("div").click(function () {
// insertAfter
$("<p>You should follow GeeksForGeeks</p>").insertAfter("p");
});
});
</script>
</head>
<body>
<p>To learn jQuery : </p>
<div>Click here to complete</div>
</body>
</html>
输出:
例子2:在这个例子中,内容将在点击 <div>
元素。
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
(document).ready(function () {
("div").click(function () {
// insertAfter
$("<p>You should follow GeeksForGeeks</p>").insertAfter("p");
});
});
</script>
</head>
<body>
<p>To learn jQuery : </p>
<p> To learn coding : </p>
<div>Click here to complete</div>
</body>
</html>
输出: