什麼是 jQuery 選擇器?


jQuery 選擇器是一個利用表示式根據給定的條件從 DOM 中找出匹配元素的函式。

選擇器用於使用 jQuery 選擇一個或多個 HTML 元素。一旦選擇了一個元素,我們就可以對該選定元素執行各種操作。jQuery 選擇器以美元符號和括號開頭 - $()。您可以嘗試執行以下程式碼以瞭解如何使用 jQuery 選擇器 −

例子

線上示例

<html>

   <head>
      <title>jQuery Selectors</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

      <script>
         $(document).ready(function() {
            $("p").css("background-color", "yellow");
         });
      </script>
   </head>
   
   <body>
      <div>
         <p class = "myclass">This is a paragraph.</p>
         <p id = "myid">This is second paragraph.</p>
         <p>This is third paragraph.</p>
      </div>
   </body>
   
</html>

更新日期:2020 年 2 月 13 日

193 次瀏覽

開啟你的 職業

完成課程並獲得認證

開始學習
廣告