Javascript 同时更改按钮值和颜色
我试图改变点击值和颜色也。我可以通过js更改值,但无法更改颜色。 这是我的Js代码Javascript 同时更改按钮值和颜色,javascript,php,jquery,Javascript,Php,Jquery,我试图改变点击值和颜色也。我可以通过js更改值,但无法更改颜色。 这是我的Js代码 <script type="text/javascript"> function change(el) { if (el.value === "P") el.value = "A"; else el.value = "P"; } </script> 功能改变(el){ 如果(el.value
<script type="text/javascript">
function change(el) {
if (el.value === "P")
el.value = "A";
else
el.value = "P";
}
</script>
功能改变(el){
如果(el.value==“P”)
el.value=“A”;
其他的
el.value=“P”;
}
这是按钮:
<td>
<input id="press5" name="press5" type="button" value="<?php echo $ro5[$_SESSION['dyy']] ?>"
onclick="return change(this);" onBlur="checkAvailability5()" class="btn btn-primary"/>
</td>
使用dom样式
对象
此代码将更改输入按钮的背景色、字体颜色和值。工作示例
功能改变(el)
{
如果(el.value==“P”){
el.value=“B”;
el.style.backgroundColor=“浅蓝色”;
el.style.color=“蓝色”;
}否则{
el.value=“P”;
el.style.backgroundColor=“浅粉色”;
el.style.color=“红色”;
}
}
背景色?Font Color?背景色侧注,我确信您不需要在onclick=“return change(this);”
中使用return
onclick=“change(this);”
应该足够了,因为el.value
正在赋值。if
和else
中缺少大括号。是的,这很简单。
function change( el )
{
if ( el.value === "P" ){
el.value = "A";
el.style.backgroundColor = "red";
} else {
el.value = "P";
el.style.backgroundColor = "blue";
}
}
<input type="button" value="P" onclick="return change(this);"/>
<script type="text/javascript">
function change( el )
{
if ( el.value === "P" ){
el.value = "B";
el.style.backgroundColor = "lightblue";
el.style.color = "blue";
}else{
el.value = "P";
el.style.backgroundColor = "lightpink";
el.style.color = "red";
}
}
</script>