jQuery after()方法

jQuery after()方法

after()方法是jQuery的一个内置函数,用于插入内容,由参数指定的匹配元素集合中的每个选定的元素。

语法:

$(selector).after(A);

参数:它接受一个参数 “A”,它是传递给方法的一个内容或函数。

返回值:它返回带有修改的选定元素。

例子1:在下面的例子中,元素被传递到方法中,它将被添加到选定的元素之后。

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <script src=
"https://code.jquery.com/jquery-1.10.2.js">
    </script>
    <style>
        p {
            background: lightgreen;
            display: block;
            width: 150px;
            padding: 10px;
            border: 2px solid green;
        }
    </style>
</head>
  
<body>
    <p>I would like to say: </p>
  
    <script>
        $("p").after("<b><h2>Hello Geeks</h2></b>"); 
    </script>
</body>
  
</html>

输出:

jQuery after()方法

例子2:在下面的代码中,该函数被传递给在选定元素后工作的方法。

<!DOCTYPE html>
<html>
  
<head>
    <meta charset="utf-8">
    <script src=
"https://code.jquery.com/jquery-1.10.2.js">
    </script>
    <style>
        p {
            background: lightgreen;
            width: 250px;
            padding: 10px;
            border: 2px solid green;
        }
    </style>
</head>
  
<body>
    <p>This is first paragraph !!!</p>
    <p>This is the second paragraph !!! </p>
  
    <script>
        $("p").after(function () {
            return "<div>" + "GeeksforGeeks" + "</div>";
        }); 
    </script>
</body>
  
</html>

输出:

jQuery after()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程