jQuery insertAfter()方法

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>

输出:

jQuery insertAfter()方法

例子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>

输出:

jQuery insertAfter()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程