如何使用 jQuery 更改背景影像?


要使用 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>

更新於:2019-12-11

3K+ 瀏覽次數

開啟你的 職業生涯

完成課程獲取認證

開始
廣告