2013-05-07 44 views
1

我有一个按钮,baeckground哦这个按钮设置与CSS这样的:如何使用javascript更改按钮背景?

background-image:url('/images/button_1_normal.png'); 

我想换用JavaScript按钮的背景。我尝试了这个选项,但它不起作用。

document.getElementById(step2).style.backgroundImage="url('images/button_1_active.png') no-repeat"; 

什么问题?谢谢^^

回答

2

我觉得你是想这样的事情。由CSS设置的第一个图像被重复,因为没有其他的顺序。但是,使用JavaScript改变的时候,你也想 “不重复”

CSS

#button { 
    background-image: url('http://imageshack.us/a/img856/3817/ticklf.png'); 
} 

HTML

<button id="button">Button</div> 

的Javascript

setTimeout(function() { 
    var button = document.getElementById("button"); 

    button.style.backgroundImage = "url('http://imageshack.us/a/img822/1917/crossn.png')"; 
    button.style.backgroundRepeat = "no-repeat"; 
}, 2000); 

jsfiddle

4

no-repeat无效。只有URL部分是背景图像属性的有效值。

要么删除或更改分配background

document.getElementById(step2) 
    .style.background="url('images/button_1_active.png') no-repeat";