Jquery没有向div添加背景图像

时间:2012-12-03 14:14:06

标签: javascript jquery background

我有一个向我的div添加背景图像的功能。但它没有表现出来。

这是我的功能

var totalCount = 6;
function changeBackground()
{
    var num = Math.ceil( Math.random() * totalCount );
    backgroundUrl = '/background/test2/'+num+'.jpg';

    $('#background').css('background-image, url('  +backgroundUrl+  ')');
}

changeBackground();

每次刷新页面时图像都会更改。我知道那部分是有效的,因为如果我改变了

$('#background').css('background-image, url('  +backgroundUrl+  ')');

document.body.parentNode.style.backgroundImage = 'url(background/test2/'+num+'.jpg)';
它向我显示了我想要的图像。但是在html标签上。我希望它在div上,所以我可以用jQuery淡化它。

这是我的CSS

#background {
width:100% !important;
height:100% !important;
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
    background-size: cover;
}

我在控制台中没有任何错误,当我查找css时,css中没有添加任何内容。我现在该怎么办?因此,在未来,我可以为自己解决问题所在。

1 个答案:

答案 0 :(得分:4)

更改

$('#background').css('background-image, url('  +backgroundUrl+  ')');

$('#background').css('background-image', 'url('  +backgroundUrl+  ')');
相关问题