如何使用 jQuery 在元素後追加元素?


要使用 jQuery 在元素後追加元素,請使用 insertAfter() 方法。

示例

你可以嘗試執行以下程式碼來學習如何使用 jQuery 在元素後追加元素

即時演示

<html>

   <head>
      <title>The jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/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>

更新於: 15-Jun-2020

2K+ 瀏覽量

開啟你的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.