身体背景图像更改在Firefox中不起作用

时间:2014-11-10 20:10:08

标签: javascript background-image

我正在尝试使用javascript更改背景图像,通过点击链接的图像,工作正常,没有任何问题在IE但它不能在Firefox中工作...请你帮忙,纠正我有什么问题? ?代码如下:

使用Javascript:

<script language="JavaScript">

var w=screen.width
var h=screen.height

if(w==1152) {
var backImage = new Array(); // don't change this

var i = 0;
backImage[0] = "images/patterns/background-1152x-bellow.png";
backImage[1] = "images/patterns/background-1152x-mellow.png";
backImage[2] = "images/patterns/background-1152x-mix-1.png";
backImage[3] = "images/patterns/background-1152x-mix-2.png";
backImage[4] = "images/patterns/background-1152x-sellow.png";
backImage[5] = "images/patterns/background-1152x-yellow.png";

function changeBGImage(whichImage){
if (document.body){
document.body.style.backgroundImage = "url("+backImage[whichImage]+")";
    cookie.set('bgImage', whichImage, 365);
}}
} else if(w==1280) {
var backImage = new Array(); // don't change this

var i = 0;
backImage[0] = "images/patterns/background-1280x-bellow.png";
backImage[1] = "images/patterns/background-1280x-mellow.png";
backImage[2] = "images/patterns/background-1280x-mix-1.png";
backImage[3] = "images/patterns/background-1280x-mix-2.png";
backImage[4] = "images/patterns/background-1280x-sellow.png";
backImage[5] = "images/patterns/background-1280x-yellow.png";

function changeBGImage(whichImage){
if (document.body){
document.body.style.backgroundImage = "url("+backImage[whichImage]+")";
    cookie.set('bgImage', whichImage, 365);
}}
}
</script>

<a href='javascript:changeBGImage(0)' ><img src='images/patterns/yellow.png' style='width:26;height:26;'></a>
<a href='javascript:changeBGImage(1)' ><img src='images/patterns/blue.png' style='width:26;height:26;'></a>
<a href='javascript:changeBGImage(2)' ><img src='images/patterns/green.png' style='width:26;height:26;'></a>
<a href='javascript:changeBGImage(3)' ><img src='images/patterns/red.png' style='width:26;height:26;'></a>
<a href='javascript:changeBGImage(4)' ><img src='images/patterns/brown.png' style='width:26;height:26;'></a>
<a href='javascript:changeBGImage(5)' ><img src='images/patterns/grey.png' style='width:26;height:26;'></a>

2 个答案:

答案 0 :(得分:0)

使用

document.body.style.background = "url("+backImage[whichImage]+")";

不是backgroundImage

答案 1 :(得分:0)

图片网址必须在CSS中作为字符串,因此您需要额外的引号(在这种情况下为单引号):

document.body.style.backgroundImage = "url('"+backImage[whichImage]+"')";

这些双引号只是使它成为javascript的字符串,而不是CSS。