如何使用 jQuery 根據其類名選擇一個元素?
元素類選擇器將選擇所有與元素給定類匹配的元素。使用 css() 方法來更改背景顏色。
你可以嘗試執行下面的程式碼來學習如何使用 jQuery 根據其類名選擇一個元素
<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() { $(".big").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>
廣告