如何定位特定型別元素的所有後代元素?


可以使用 find( 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() {
            $("p").find("span").addClass("selected");
         });
      </script>
       
      <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>

更新於: 09-12-2019

87 次瀏覽

開啟你的 職業生涯

完成教程獲得認證

開始
廣告
© . All rights reserved.