解釋 jQuery.append()、jQuery.prepend()、jQuery.after() 和 jQuery.before() 方法。


jQuery.append()

append( content ) 方法將內容追加到每個匹配元素的內部。以下是此方法使用到的所有引數的說明:

  • content − 要在每個目標之後插入的內容。可以是 HTML 或文字內容

示例

您可以嘗試執行以下程式碼來了解如何使用 jQuery.append() 方法

線上演示

<html>

   <head>
      <title>jQuery append() 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 () {
               $(this).append('<div class = "div"></div>' );
            });
         });
      </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" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
   
</html>

jQuery.prepend()

prepend( content ) 方法將內容預先新增到每個匹配元素的內部。以下是此方法使用到的所有引數的說明:

  • content − 要在每個目標之後插入的內容。可以是 HTML 或文字內容

示例

您可以嘗試執行以下程式碼來了解 prepend() 方法

線上演示

<html>

   <head>
      <title>jQuery prepend() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function () {
               $(this).prepend('<div class = "div"></div>' );
            });
         });
      </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" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
   
</html>

jQuery.after()

after( content ) 方法在每個匹配元素之後插入內容。以下是此方法使用到的所有引數的說明:

  • content − 要在每個目標之後插入的內容。可以是 HTML 或文字內容

示例

您可以嘗試執行以下程式碼來了解如何在 jQuery 中使用 after() 方法

線上演示

<html>

   <head>
      <title>jQuery after() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function () {
               $(this).after('<div class = "div"></div>' );
            });
         });
      </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" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
   
</html>

jQuery.before()

before( content ) 方法在每個匹配元素之前插入內容。以下是此方法使用到的引數說明:

  • content − 要在每個目標之前插入的內容。可以是 HTML 或文字內容

示例

您可以嘗試執行以下程式碼來了解如何在 jQuery 中使用 before() 方法

線上演示

<html>

   <head>
      <title>jQuery before() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("div").click(function () {
               $(this).before('<div class = "div"></div>' );
            });
         });
      </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" style = "background-color:blue;"></div>
      <div class = "div" style = "background-color:green;"></div>
      <div class = "div" style = "background-color:red;"></div>
       
   </body>
</html>

更新於: 2019年12月10日

238 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.