如何使用 jQuery 將前幾項包裝在 div 中?


要將前幾項包裝在 div 中,請使用 lt 選擇器,然後使用 wrapAll() 方法進行包裝。你可以嘗試執行以下程式碼,瞭解如何使用 jQuery 將前幾項列表項包裝在 div 中 −

示例

實際演示

<html>
   <head>
      <title>jQuery wrap() 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(4)').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>
         <li>Nepal</li>
         <li>Bhutan</li>
      </ul>

   </body>
</html>

更新時間: 14-2 月-2020

579 次瀏覽

開啟你的 事業

完成此課程認證

立即開始
廣告
© . All rights reserved.