简体   繁体   中英

Trying to change background image, by clicking on an image, jQuery, HTML, CSS

I have images I photoshopped that are weather icons like a sun, cloud, rainy cloud, etc. On the Site I want to be able to click on them, and change the background image of the site, the Body background. But it doesn't work, can't click on the image, nothing happens. This is what I have so far, with #skyclear being the image ID, and clearsky being the css styling =>

$('body').addClass(bodyClass);

$('#skyclear').on('click', () => {
    bodyClass = 'clearsky'
})
$('body').addClass(bodyClass);

This should be in the onClick function. You should also consider removing any unnecessary classes inside of the onClick as well.

I would just do it like this:

$('#skyclear').on('click', function(){
    $('body').addClass('clearsky');
});

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