jQuery的背景从图像到颜色,然后再次返回 [英] jquery background from image to color and back again

查看:104
本文介绍了jQuery的背景从图像到颜色,然后再次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html页面,已加载jquery.目前没有CSS.

I have a html page, with jquery loaded. It has no css at the moment.

它的背景图片设置为:

<body background="image1.gif">

我正在尝试将其更改为颜色(例如黑色),然后使用onclick再次更改为图像.

I'm trying to change this to a color (say black) and back again to the image using onclick.

我设法设置了div,一切正常.为了将颜色更改为颜色,我使用了:

I've managed to set up the divs and all ok. For changing color to color I used:

jQuery('body').css('background-color','black');

在另一页上,但这在这里不起作用,因为我想从图像转到彩色然后再次返回.

on another page, but that doesn't work here because i want to go from image to color and back again.

那么,如何将背景图像更改为纯色并使用jquery再次返回?

So, how can I change the background image to a flat color and back again with jquery?

推荐答案

使用CSS并更改类:

HTML:

<body class="image">

CSS:

body.image{
  background-image: url('image.gif');
}
body.colour{
  background-color: black;
}

对于脚本:

$('body').addClass('colour').removeClass('image');

然后再次返回:

$('body').addClass('image').removeClass('colour');

这篇关于jQuery的背景从图像到颜色,然后再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆