如何使用 jQuery 更改 CSS?


要使用 jQuery 更改 CSS,請使用 jQuery css() 方法。

示例

你可以嘗試執行以下程式碼來使用 jQuery 更改 CSS

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("h2").css("backgroundColor", "red");

    $("#myid").css({
        "backgroundColor": "gray",
        "color": "white"});

    $(".myclass").css("border", "2px solid black");

});
</script>

</head>
<body>
<h2>Heading 2</h2>
<p id="myid">This is some text.</p>
<div class="myclass">This is some text.</div>

</body>
</html>

更新於:2019 年 12 月 10 日

627 次瀏覽

踢 start 您的 職業

完成課程認證

開始學習
廣告
© . All rights reserved.