javascript改变按钮颜色,点击a按钮时a变色,点击b时b变色a恢复原色 谢谢了

如题所述

在onclick事件中修改按钮的样式,最简单的办法是使用jquery实现追问

麻烦您帮我写下代码 谢谢了

追答

document.getElementById('a').style=新样式

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style>
.btn{width:50px; height:20px; background-color:#900;}
</style>
<script>
function changeColor(num){
for(var i=1;i<=2;i++){
var str = document.getElementById('sub_'+i);
if(i==num){
str.className="btn";
}else{
str.className="";
}
}
}
</script>
<body>
<div><input type="submit" name="sub_a" value="A" id="sub_1" onclick="changeColor('1')"></div>

<div><input type="submit" name="sub_b" value="B" id="sub_2" onclick="changeColor('2')"/></div>
</body>
</html>

你自己测试下,如果是多个的情况下,就要多带个参数进去!本回答被提问者和网友采纳
相似回答