如何使用 jQuery 更改背景颜色?

jqueryweb developmentfront end technology

要使用 jQuery 更改背景颜色,请使用 jQuery 的 css() 属性。我们将使用 jQuery 的 on() 和 css() 方法在鼠标悬停时更改背景颜色。

示例

您可以尝试运行以下代码来了解如何使用 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");
        },  
       
        mouseleave: function(){
            $(this).css("background-color", "red");
        },
       
        dblclick: function(){
            $(this).css("background-color", "yellow");
        }
    });
});
</script>
</head>
<body>

<p>Double click and move the mouse pointer to change the background color.</p>

</body>
</html>

相关文章