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


insertAfter( selector ) 方法將所有匹配的元素插入到另外指定的一組元素後。

以下是此方法使用的所有引數的說明 −

  • selector − 所選元素插入後的內容。

示例

您可以嘗試執行以下程式碼以瞭解如何在 jQuery 中使用 jQuery.insertAfter() 方法 −

實際演示

<html>

   <head>
      <title>jQuery insertAfter() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/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>

更新於: 14-Feb-2020

132 次觀看

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告