2013-05-07 35 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";