简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM