我正在尝试根据一个在高中时读到的短篇小说来制作一个简单的JS“游戏”,其中涉及一个小岛上的机器人螃蟹。或多或少将是一个模拟器,玩家可以在其中设置初始参数,然后点击开始并观看一切。
我的问题是我似乎无法更改mainWindow div的innerHTML,这是我以前从未真正遇到过的问题。
该代码不会引发任何错误,但是当我在浏览器中打开检查器时,它表明mainWindow div仅是空的,尽管事实表明crab对象本身正在按预期工作。
提前感谢您的帮助,希望这不是一个愚蠢的事情...
〜KP
下面包含的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Crabs!</title>
<head>
<script>
var theCrabs = [new crab(12, 12, new Vector2(512, 384), "alive")];
var numCrabs = 2;
var islandSizeX = 1024;
var islandSizeY = 768;
var theTime = new clock(12, 0, 0);
var fps = 60;
var go = false;
function Vector2 (_x, _y)
{
this.x = Number(_x);
this.y = Number(_y);
}
function crab (_size, _speedmax, _position, _status = "alive")
{
this.size = _size;
this.speedmax = _speedmax;
this.speed = new Vector2(0, 0)
this.position = _position;
this.target = new Vector2(0, this.position.y)
this.status = _status;
this.xMoment = 0;
this.yMoment = 0;
this.Move = function (_murder = false) {
if (this.status == "alive") {
if (this.position.x > this.target.x) {
this.speed.x = this.speedmax * -1;
} else if (this.position.x < this.target.x) {
this.speed.x = this.speedmax;
} else {
this.speed.x = 0;
}
if (this.position.y > this.target.y) {
this.speed.y = this.speedmax * -1;
} else if (this.position.y < this.target.y) {
this.speed.y = this.speedmax;
} else {
this.speed.y = 0;
}
this.position.x += this.speed.x;
this.position.y += this.speed.y;
}
if (this.position.x < 0) {
this.position.x = 0;
this.target.x = islandSizeX;
}
if (this.position.x > (islandSizeX - this.size)) {
this.position = (this.islandSizeX - this.size);
this.target.x = 0;
}
if (this.position.y < 0) { this.position.y = 0; }
if (this.position.y > (islandSizeY - this.size)) { this.position = (this.islandSizeY - this.size); }
}
this.getColorByStatus = function () {
(REMOVED FOR BREVITY)
}
}
function draw ()
{
//clear the window
document.getElementById("mainWindow").innerHTML = "";
//draw clock
document.getElementById("mainWindow").innerHTML += "<div style='position:relative; left:10px; top:10px; color:black;>" + theTime.getTime() + "</div>";
//draw crabs
for (var i=0; i<theCrabs.length; i++) {
currentCrab = theCrabs[i];
document.getElementById("mainWindow").innerHTML += "<div style='width:" + currentCrab.size + "; height:" + currentCrab.size + "; position:relative; left:" + currentCrab.position.x + "px; top:" + currentCrab.position.y + "px; background-color:" + "rgb(200, 90, 110)" + ";></div>";
}
}
function clock (_h, _m, _s)
{
(REMOVED FOR BREVITY)
}
function tick ()
{
if (go) {
for (var i=0; i<theCrabs.length; i++) {
theCrabs[i].Move();
}
theTime.addTime(0, 0, 10);
draw();
}
}
function FPS () { return Math.round(1000 / fps); }
function startstop()
{
if (!go) {
go = true;
document.getElementById("btnStart").innerHTML = "STOP";
setInterval(tick, FPS());
} else {
go = false;
document.getElementById("btnStart").innerHTML = "START";
clearInterval(tick);
}
}
</script>
<style> body { background-color: rgb(0, 128, 200); } </style>
</head>
<body>
<div id="foundation" style="height:100%; width:100%; border: 0px white solid;">
<div id="mainWindow" style="height:768px; width:1024px; border: 2px black solid; background-color: rgb(255, 250, 175);">
</div>
<button id="btnStart" onclick="startstop()">START</button><br/><br/>
<span style="font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; font-size: 24pt; color: white;"> Crabs on the island. </span>
</div>
</body>
</html>
答案 0 :(得分:3)
在关闭附加的内容样式之前,您错过了引号'
document.getElementById("mainWindow").innerHTML += "<div style='position:relative; left:10px; top:10px; color:black;'>" + theTime.getTime(+ "</div>";
只需在color:black;'
和... + "rgb(200, 90, 110)" + ";'></div>";
之后添加
为避免任何其他问题,请尝试将async
属性添加到script
标记中,请使用
document.querySelector("#mainWindow").innerHTML="you content ..."
或删除脚本内容并将其放置在关闭body
标签之前
</body>
...
<script>
<!--your script-->
</script>
</body>