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

2 回答 2

4

缺少右括号(在 之后"bg green")。

if(document.getElementById('cmdinput').value.substring(0,9) == "bg green") {
document.getElementById('a').style.backgroundColor = 'green';
}
于 2013-09-30T00:58:12.987 回答
2

试试这个:

document.body.style.background = 'green';
于 2013-09-30T00:58:01.080 回答