如何使用 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>

更新於: 2020-02-14

579 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.