如何利用 jQuery 根據其 ID 屬性選擇元素


元素 ID 選擇器選擇具有給定 ID 屬性的單個元素。以下是選擇方法

$('#elementid')

您可以嘗試執行以下程式碼,瞭解如何使用 jQuery 根據 ID 屬性選擇元素

線上演示

<html>
   <head>
      <title>jQuery Selector</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function() {
            $("#div2").css("background-color", "yellow");
         });
      </script>
   </head>
   <body>
      <div class = "big" id = "div1">
         <p>This is first division of the DOM.</p>
      </div>
      <div class = "medium" id = "div2">
         <p>This is second division of the DOM.</p>
      </div>
      <div class = "small" id = "div3">
         <p>This is third division of the DOM</p>
      </div>
   </body>
</html>

更新於: 2020 年 6 月 12 日

349 人次瀏覽

開啟 職業生涯

透過完成課程獲得認證

開始
廣告