我在网页上有多个按钮。但尝试在每次点击时更改单个按钮的按钮颜色。我的代码如下。但不工作....任何帮助?按钮颜色不会改变多个按钮的切换
<html>
<head>
<script type="text/javascript">
function toggleColor(id) {
if(document.getElementById(id).style.background !== 'rgb(255,0,0)') {
document.getElementById(id).style.background = '#FF0000';
}
else {
document.getElementById(id).style.background = '#00FF00';
}
}
</script>
</head>
<body>
<button type="button" id="1" style="background-color:#00FF00;" onclick="toggleColor(this.id)">1</button>
</body>
你的代码看起来就像是在学习会话中间:)我说你还没有准备好测试自己,只要坚持下去。 (不想听起来像冲洗,但它是一种更好的学习方式) – Nenotlep
因为这个'background!=='rgb(255,0,0){',你可能会在你的代码上得到一个sintax错误。尝试将其更改为'background!=='#ff0000'){' – DontVoteMeDown
对不起,我的错误...但即使#ff0000它不工作.... –