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


wrapInner() 方法用一個 DOM 元素包裝每個匹配元素(包括文字節點)的內部子元素內容。

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

  • html − 一個 DOM 元素,它將包裝在目標的周圍。

示例

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

現場演示

<html>

   <head>
      <title>jQuery wrapInner() 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).wrapInner(document.createElement( "b" ));
            });
         });
      </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 = "destination">THIS IS TEST</div>
      <div class = "div" style = "background-color:blue;">ONE</div>
      <div class = "div" style = "background-color:green;">TWO</div>
      <div class = "div" style = "background-color:red;">THREE</div>
       
   </body>
   
</html>

更新於: 2020 年 2 月 14 日

82 次訪問

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告