我正在使用下面的代碼來生成canvas
。目前它將畫布元素添加到body
,我如何才能將它添加到名爲「divGameStage」的div
,該元素不嵌套在除body
之外的任何其他元素中。如何在DIV中添加html5帆布
編輯:我嘗試的第一個建議,但沒有畫布出現,完整的代碼如下:
<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>
[將canvas添加到使用javascript的頁面]可能的副本(http://stackoverflow.com/questions/9152224/add-canvas-to-a-page-with-javascript) – Vadzim 2016-06-04 13:06:04