2014-04-11 77 views
0

如何使內聯css更改顏色或輸入按鈕的圖像onclick?Onclick從內聯css和JavaScript更改按鈕圖像

<div><input type="button" id="btn" style="background-image:url('old.png');width:36px;height:36px;" onclick="change();"></div> 

function change() { 
    document.getElementById("btn").style.backgroundImage=url('new.png'); 
} 

如何使用JavaScript來完成此操作?我錯過了一些...

或者是否有可能是一個HTML或CSS的解決方案更容易?

+0

你需要在你的函數的document.getElementById(「BTN」)添加引號style.backgroundImage =「URL(‘new.png’)」。 – pconnor88

回答

0
<input type="button" id="btn" style="background-color:green;width:36px;height:36px;" onclick="change();"> 
+0

對不起,我錯過了那部分。我編輯了我的問題。它不工作,雖然我現在已經onclick() – mpavlovic89

0

,需要相應地包串。

document.getElementById("btn").style.backgroundImage=url('new.png'); 

應該

document.getElementById("btn").style.backgroundImage="url('new.png')"; 
0

小提琴http://jsfiddle.net/Anee/cUTj8/1/

你有type="button"。如果你把它的按鈕應該是onclick,而不是onchange

<input type="button" id="btn" style="background-color:green;width:36px;height:36px;" onclick="change(this);"> 


function change(myinput) { 
    myinput.style.backgroundImage= "url('new.png')"; 
}