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


wrapAll() 方法將匹配集中所有元素包裝到單個包裝元素中。

此處是此方法使用所有引數的說明 -

  • elem - 將圍繞目標包裝的 DOM 元素。

例項

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

即時演示

<html>
   <head>
      <title>jQuery wrapAll() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function() {
           $('ul.myclass > li:lt(2)').wrapAll('<div class="demo"></div>')
         });
      </script>

      <style>
         .demo {
            border: 3px dashed blue;
            margin: 5px;
         }      
      </style>

   </head>
   <body>

      <ul class="myclass">
         <li>India</li>
         <li>US</li>
         <li>UK</li>
         <li>Australia</li>
         <li>Bangladesh</li>
      </ul>
   </body>
</html>

更新於: 2020 年 2 月 14 日

124 次瀏覽

開啟你的 職業

完成課程獲取認證

開始學習
廣告