更改页面的背景颜色

时间:2013-09-30 00:53:20

标签: javascript html

我正在尝试创建命令行,当您键入命令时,例如'bg green',背景变为绿色。一切正常,除非你输入'bg green',按回车键,没有任何反应。背景保持黑色。这是我的代码:

<html>  
<body style="background-color:black" id="a">
<div style="width:100%;bottom:0px;height:30px;position:absolute;"></div>
<div id="commandline" onkeydown="if (event.keyCode == 13) command(); " style="-webkit-transform:rotate(0deg);font-family: 'Kelly Slab', cursive;z-index:2;background-color:black;height:36px;width:100%;bottom:0px;position:absolute;color:white;left:0px;font-size:20px;">
&gt
<input style="font-family:'Kelly Slab';color:white;height:100%;width:100%;background-color:black;border:none;position:absolute;font-size:30px;left:20px;top:0px;" id="cmdinput"></input>
<div id="commands"  style="z-index:1;background-color:black;height:100%;width:100%;bottom:36px;position:absolute;left:0px;"></div>
</body>
</html>    

<script type="text/javascript">

function command(){
if(document.getElementById('cmdinput').value.substring(0,9) == "bg green"{
document.getElementById('a').style.backgroundColor = 'green';
}
}

</script>    

我认为问题在于javascript,但我不确定。有谁知道这是什么问题?

2 个答案:

答案 0 :(得分:4)

缺少结束括号("bg green"之后)。

if(document.getElementById('cmdinput').value.substring(0,9) == "bg green") {
document.getElementById('a').style.backgroundColor = 'green';
}

答案 1 :(得分:2)

请改为尝试:

document.body.style.background = 'green';
相关问题