简体   繁体   English

如何使用jQuery更改按钮单击时的背景图像?

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

I'm trying to do something very simple: have the background of a website change to a different image upon a button click. 我正在尝试做一个非常简单的事情:单击按钮,将网站的背景更改为其他图像。

I have been trying to get this to work for hours and the background just does not change. 我一直在努力使其工作数小时,而背景却没有改变。 I checked the directory of my images. 我检查了图像目录。 Everything is correct, there is just something wrong with my code and I don't see what it is: 一切都正确,我的代码出了点问题,我看不出它是什么:

 $("#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> 

Looks like you have a syntax error in your JavaScript 看起来您的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