如何使用 jQuery 設定背景色?


要使用 jQuery 設定背景色,請使用 jQuery css() 屬性。我們將在滑鼠懸停時使用 jQuery on() 方法設定背景色。

示例

可以嘗試執行以下程式碼來在 jQuery 中設定背景色。

線上演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("body").on({
        mouseenter: function(){
            $(this).css("background-color", "gray");
        },  
    });    
});
</script>
</head>
<body>
<p>Move the mouse pointer on the page to change the background color.</p>
</body>
</html>

更新於: 2019 年 12 月 11 日

6K+ 瀏覽量

啟動你的職業

完成本課程獲得認證

開始
廣告
© . All rights reserved.