当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery insertAfter()用法及代码示例

jQueryinsertAfter()方法用于在指定元素后插入一些 HTML 内容。 HTML 内容将在每次出现指定元素后插入。

用法:

$('content_to_be_inserted').insertAfter(target)

参数:

它采用一个参数“target”,该参数指定在此目标元素之后,需要将传递的内容插入到 HTML 文档中。

返回值:

它不返回任何值。

示例 1:在此示例中,将通过单击 div 元素将内容添加到 <p> 标记之后。

HTML


<!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 元素将内容添加到所有 <p> 标记之后。

HTML


<!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>

输出:



相关用法


注:本文由纯净天空筛选整理自ShivamKD大神的英文原创作品 jQuery insertAfter() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。