簡體   English   中英

如何使用jQuery更改按鈕單擊時的背景圖像?

[英]How to change background image on button click with jquery?

我正在嘗試做一個非常簡單的事情:單擊按鈕,將網站的背景更改為其他圖像。

我一直在努力使其工作數小時,而背景卻沒有改變。 我檢查了圖像目錄。 一切都正確,我的代碼出了點問題,我看不出它是什么:

 $("#boots").on("click", function() { $(".intro").css("background-image", "url(image/1.jpg)"); }); }); 
 html, body { margin: 0; padding: 0; height: 100%; width: 100%; font-size: 16px; } .intro { width: 100%; height: 100%; margin: auto; background-image: url(image/0.jpg); background-repeat: no-repeat; background-size: cover; } 
 <head> <link rel="stylesheet" type="text/css" href="a.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src = "b.js"></script> </head> <body> <section class ="intro"> <div class="inner"> <div class="content"> <input class="aButton" type="button" id="boots" /> </div> </div> </section> </body> 

看起來您的JavaScript中有語法錯誤

$("#boots").on("click", function() {
    $(".intro").css("background-image", "url(image/1.jpg)");
    }); // <-- shouldn't be there
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM