如何使用 jQuery 選中具有多個類別的元素?


要選擇具有多個類的元素,請執行以下操作。在此,x、y 和 z 是我們的類。

$('.x.y.z')

你可以嘗試執行以下程式碼,來學習如何在 jQuery 中選擇具有多個類的元素。

即時演示

<html>
   <head>
      <title>jQuery Selector Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
   
      <script>
         $(document).ready(function() {
            $(".one.two").css("background-color", "grey");
         });
      </script>
   </head>
   
   <body>

      <div class = "one two" id="div">
         <p>This is first division of the DOM.</p>
      </div>

      <div class = "three" id = "div3">
         <p>This is third division of the DOM</p>
      </div>

   </body>    
</html>

更新於: 2020 年 6 月 12 日

602 次瀏覽

啟動你的 事業

完成課程獲得認證

開始
廣告
© . All rights reserved.