2012-08-30 14 views
0

我想在javascript函數中更改html文檔背景圖像,但我遇到了一些困難。這是我有:在javascript函數中更改文檔背景圖像

function changeBgImage(){ 

    document.background = "url('images/SecondBackground.png')"; 

} 

但正如我所說,不工作..任何想法?

回答

2

使用

document.body.style.backgroundImage = "url('images/SecondBackground.png')"; 
+0

,完美的工作:)謝謝@Amol Kolekar – Tiwaz89

+0

歡迎您.. :) –

0

你接近,但你需要。體爲好。

document.body.style.background = "url('images/SecondBackground.png')"; 
1
function changeBGImage() { 
    document.body.style.backgroundImage = "url(http://images.cheezburger.com/completestore/2010/7/9/fe8e91c5-c3f1-40cf-a034-983e8683ba73.jpg)"; 
}; 

這會做到這一點!

+1

@ Neurofluxation - 哈哈!懸停喇嘛:) – Tiwaz89

+0

:D ohhhh yeahhhhhh –

0

嘗試是這樣的

   document.getElementById("id of body").setAttribute("style","background-url:'images/SecondBackground.png'")