2013-09-30 150 views
0

我正在尝试创建一个命令行,其中当您键入命令时,如'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,但我不知道。有谁知道这个问题是什么?

回答

4

缺少右支架("bg green"之后)。

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

谢谢,但它仍然没有解决它。 –

+0

@CJ Cohorst它必须工作。 – Alex

+0

等等,没关系。它确实修复了它。我只是在试图改变它的时候搞砸了一些东西。非常感谢! –

2

尝试此代替:

document.body.style.background = 'green'; 
+0

我改变了它,但它仍然不会改变背景,当我按enter键。 –