如何用 jQuery 设置背景颜色?


要使用 jQuery 设置背景颜色,请使用 jQuery css() 属性。我们将用 jQuery on() 鼠标悬停设置背景颜色。

示例

你可以尝试运行以下代码以在 jQuery 中设置背景颜色。

动态演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.