如何动态更改图像?

时间:2014-02-28 07:47:22

标签: javascript html5 css3

我正在制作一个小横幅的东西,我的主图像没有显示

代码似乎工作正常,但没有任何显示。

非常感谢任何帮助。

var urlString = 'url(images/mainphoto' + ss_i + '.jpg)';
var obj = document.getElementById("mainphoto");
obj.style.background = urlString;
//document.getElementById("mainphoto").style.backgroundImage = "url('images/mainphoto" + ss_i + ".jpg')";
console.log(urlString);
<div id="mainphoto"></div>
#mainphoto {
    background:url(images/mainphoto0.jpg);
    position: absolute;
    margin-top: 149px;
    z-index: 0;
}

1 个答案:

答案 0 :(得分:2)

div为空,因此我猜不出来,尝试将widthheight添加到div的风格