如何在javascript中创建OK按钮

如何在javascript中创建OK按钮,javascript,html,Javascript,Html,我有2个图像和4个按钮。我想改变按钮的颜色和点击按钮时图像的背景 function button_fn(Obj) { document.getElementById("div_field").style = "background-color:" + Obj.name; document.getElementById("img_id").style = "border-color:" + Obj.name; } </script> <body&g

我有2个图像和4个按钮。我想改变按钮的颜色和点击按钮时图像的背景

function button_fn(Obj) 
{
    document.getElementById("div_field").style = "background-color:" + Obj.name;
    document.getElementById("img_id").style = "border-color:" + Obj.name;   
}   

</script>
<body>
<fieldset>
<legend > background color change:</legend>
<div id="div_field">
<img src="home.png" name="img_png" width="200"  />
</div>
<form >
<input type="button"  name="red"  value="Red" id="but1_id" onclick= "button_fn(this)"/>
<input type="button"  name="green" value="Green" id="but2_id" onclick="button_fn(this)"/>
<input type="button"  name="blue" value="Blue" id="but3_id" onclick="button_fn(this)"/>
<input type="button"  name="confirm" value="OK" id="confirm_id" onclick="button_fn(this)"/>
</form>
</fieldset>
<div id="div_image" >
<img src="service2.jpg" id="img_id" name="imag" width="200"border="40" />

</div>
功能按钮(Obj)
{
document.getElementById(“div_字段”).style=“背景色:”+Obj.name;
document.getElementById(“img_id”).style=“边框颜色:”+Obj.name;
}   
背景颜色变化:

您必须使用确认框:

var r=confirm("Press a button");
if (r==true)
{
  x = "You pressed OK!";
}
else
{
  x = "You pressed Cancel!";
}

您所需要的就是一个牢固的按钮

var r=confirm("Press a button to choose");
if (r==true){
  x = "You pressed OK!";
}
else{
  x  ="You pressed Cancel!";
}
试一试

另一种方法是使用以下脚本:

function button_fn(Obj) 
{
    objId = Obj.id;
    document.getElementById(objId).style = "background-color:"+Obj.name;
    document.getElementById(objId).style = "border-color:"+Obj.name;
}
function button_fn(Obj) 
{
    objId = Obj.id;
    document.getElementById(objId).style = "background-color:"+Obj.name;
    document.getElementById(objId).style = "border-color:"+Obj.name;
}