从内联css和JavaScript中单击更改按钮图像

时间:2014-04-11 19:17:45

标签: javascript html css

如何制作一个内联css,可以点击更改输入按钮的颜色或图像?

<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解决方案?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你需要适当地包装字符串。

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

应该是

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

答案 2 :(得分: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')";
}