7

我正在使用下面的代码生成一个canvas. 目前它将画布元素添加到 中body,我怎样才能让它添加到一个div名为“divGameStage”,它不嵌套在任何其他元素中,除了body.

编辑:我尝试了第一个建议,但没有出现画布,完整代码如下:

<head>
<meta charset="utf-8">
<title>Game Stage</title>
<script type="text/javascript">
    function loadCanvas(id) {
        var canvas = document.createElement('canvas');
        div = document.getElementByID(id); 
        canvas.id     = "CursorLayer";
        canvas.width  = 1224;
        canvas.height = 768;
        canvas.style.zIndex   = 8;
        canvas.style.position = "absolute";
        canvas.style.border   = "1px solid";
        div.appendChild(canvas)
    }
        </script>
</head>
<body>
<header>
    <h1>The most important heading on this page</h1>
    <p>With some supplementary information</p>
</header>
<div id="divControls"></div>
<div id="divGameStage"></div>
<script type="text/javascript">
    loadCanvas(divGameStage);
</script>
</body>
</html>
4

4 回答 4

17

试试这个代码,肯定会为你工作:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Game Stage</title>
<script type="text/javascript">
    function loadCanvas(id) {
        var canvas = document.createElement('canvas');
        div = document.getElementById(id); 
        canvas.id     = "CursorLayer";
        canvas.width  = 1224;
        canvas.height = 768;
        canvas.style.zIndex   = 8;
        canvas.style.position = "absolute";
        canvas.style.border   = "1px solid";
        div.appendChild(canvas)
    }
</script>
</head>
<body>
<header>
    <h1>The most important heading on this page</h1>
    <p>With some supplementary information</p>
</header>
<div id="divControls"></div>
<div id="divGameStage"></div>
<script type="text/javascript">
    loadCanvas("divGameStage");
</script>
</body>
</html>

请记住以下几点:

  • 错误 1 ​​是 loadCanvas("divGameStage"); 中缺少引号
  • 错误 2 是语法错误 div = document.getElementById(id); “..ID(id)”在您的代码中。

如果那么它也不起作用,那么我确定您正在 Internet Explorer 上对其进行(specially < IE9)
测试

干杯!!!

于 2012-08-06T07:52:26.930 回答
4

您只需将其附加到您的<div>而不是正文中:

<script type="text/javascript">
    function loadCanvas(id) {
        var canvas = document.createElement('canvas'),
            div = document.getElementById(id);
        canvas.id     = "canvGameStage";
        canvas.width  = 1000;
        canvas.height = 768;
        canvas.style.zIndex   = 8;
        canvas.style.position = "absolute";
        canvas.style.border   = "1px solid";
        div.appendChild(canvas);
    }
    /* ... */
    loadCanvas("divGameStage");
</script>

很简单:-)

于 2012-08-05T12:38:38.500 回答
3
<script type="text/javascript">
    function loadCanvas() {
        var canvas = document.createElement('canvas');
        canvas.id     = "canvGameStage";
        canvas.width  = 1000;
        canvas.height = 768;
        canvas.style.zIndex   = 8;
        canvas.style.position = "absolute";
        canvas.style.border   = "1px solid";
        var div = document.createElement("div");
        div.className = "divGameStage";
        div.appendChild(canvas);
        document.body.appendChild(div)
    }
</script>
于 2012-08-05T12:42:58.147 回答
1

这里唯一的问题是:

loadCanvas(divGameStage);

用引号divGameStage括起来:

loadCanvas("divGameStage");

因为它需要是一个字符串才能运行getElementById

于 2012-08-05T19:11:27.563 回答