如何獲取一個元素組,其中包含每個匹配的元素集的所有唯一直接子元素?


children( [selector] ) 方法獲取包含每個匹配元素集的所有唯一直接子元素的元素集。

示例

您可以嘗試執行以下程式碼,瞭解如何獲取包含每個匹配元素集的所有唯一直接子元素的元素集

現場演示

<html>
   <head>
      <title> jQuery Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function(){
            $("div").children(".selected").addClass("blue");
         });
      </script>
       
      <style>
       .blue {
          color:blue;
        }
      </style>
   </head>
   
   <body>
      <div>
         <span>Hello</span>
         <p class = "selected">Hello Again</p>
         <div class = "selected">And Again</div>
         <p class = "biggest">And One Last Time</p>
      </div>
   </body>
   
</html>
      <style>
         .selected {
           color:blue;
         }
      </style>
   </head>
   
   <body>
      <p>This is first paragraph and <span>THIS IS BLUE</span></p>
      <p>This is second paragraph and <span>THIS IS ALSO BLUE</span></p>
   </body>
</html>

更新日期: 2019-12-09

77 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.