如何在jQuery中使用jQuery.insertAfter()方法?

insertAfter(selector)方法将所有匹配的元素插入另一个指定的元素集之后。

这是此方法使用的所有参数的描述-

  • selector 选择器 -插入所选元素之后的内容。

示例

您可以尝试运行以下代码以了解如何在jQuery中使用jQuery.insertAfter()方法-


<html>

   <head>
      <title>jQuery insertAfter() method</title>
      <script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function () {
               $("#source").insertAfter(this);
            });
         });
      </script>
       
      <style>
         .div {
            margin:10px;
            padding:12px;
            border:2px solid #666;
            width:60px;
         }
      </style>
   </head>
   
   <body>
   
      <p>Click on any square below to see the result:</p>
       
      <div class = "div" id = "source"></div>
      <div class = "div" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
   
</html>