这里是我的代码:JavaScript的的getElementById和innerHTML的不工作间接
<html>
<head>
<title>My Game Title Goes Here!</title>
<script type="text/javascript">
function startGame(){
document.getElementById("2").innerHTML = ('Testing!');
}
document.body.onload = keyListener(){
document.getElementById("1").onkeypress = startGame;
}
</script>
</head>
<body>
<div class="title" name="Game Title" id="0">Game Title</div>
<div tabindex="0" class="gamecontainer" name="Game Container" id="1">
Press any key to start.
</div>
<div class="gamemonitor" name="Game Monitor" id="2">
Game Monitor:
</div>
</body>
</html>
像我期待它(我使用谷歌浏览器)我不工作。
,如果我直接运行它,这样它仅适用于:
<div tabindex="0" class="gamecontainer" name="Game Container" id="1" onkeypress="document.getElementById('2').innerHTML = ('Testing!')">
Press any key to start.
</div>
<div class="gamemonitor" name="Game Monitor" id="2">
Game Monitor:
</div>
我检查了我的代码吨次,我无法找到任何明确的错误,像错别字或任何东西。如果那是问题,那么我很抱歉浪费你的时间,但这真是一个诡计'我。
'1'不是一个有效的'id' – gdoron
它认为第二种方式不应该工作而不是第一个,你用引号有混合... :( – gdoron
'keyListener(){}'是不是有效的语法 – SLaks