Javascript 更改页面的背景色
我正在尝试制作一个命令行,当您键入一个命令时,例如“bg green”,背景将变为绿色。除了键入“bg green”并按enter键外,一切正常。背景保持黑色。这是我的代码:Javascript 更改页面的背景色,javascript,html,Javascript,Html,我正在尝试制作一个命令行,当您键入一个命令时,例如“bg green”,背景将变为绿色。除了键入“bg green”并按enter键外,一切正常。背景保持黑色。这是我的代码: <html> <body style="background-color:black" id="a"> <div style="width:100%;bottom:0px;height:30px;position:absolute;"></div> <div id=
<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;">
>
<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>
&燃气轮机
函数命令(){
if(document.getElementById('cmdinput').value.substring(0,9)==“bg绿色”{
document.getElementById('a').style.backgroundColor='green';
}
}
我认为问题出在javascript上,但我不确定。有人知道问题出在哪里吗?试试这个:
document.body.style.background = 'green';
请尝试以下方法:
document.body.style.background = 'green';
缺少闭合括号(在“bg绿色”
之后)
缺少闭合括号(在“bg绿色”
之后)
谢谢,但它还是没修好。等等,没关系。它修好了。我只是在试图改变它时弄糟了一些东西。非常感谢!谢谢,但它还是没修好。等等,没关系。它修好了。我只是在试图改变它时弄糟了一些东西。非常感谢!我改变了它,但当我点击ent时它仍然没有改变背景呃,我改了,但是当我按回车键的时候,背景还是没变。