如何在javascript中制作一个OK按钮

时间:2014-03-03 12:30:44

标签: 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>
<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>

3 个答案:

答案 0 :(得分:0)

您必须使用确认框:

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

http://www.tizag.com/javascriptT/javascriptconfirm.php

答案 1 :(得分:0)

你只需要一个确认按钮

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

答案 2 :(得分:0)

尝试

function button_fn(Obj) 
{
    Obj.style = "background-color:"+Obj.name;
    Obj.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;
}