如何使用 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 日

620 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

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