如何使用 jQuery 更改背景图片?

jqueryweb developmentfront end technology

要使用 jQuery 更改背景图片,请使用 jQuery css() 方法。background-image 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-image", "url('/css/images/css.jpg')");
        },  
    });    
});
</script>
</head>
<body>
<p>Move the mouse pointer on the page to change the background image.</p>
</body>
</html>

相关文章