Tôi đang sử dụng mã bên dưới để tạo số canvas
. Tại thời điểm này, thêm phần tử canvas vào body
, làm cách nào để thêm nó vào một tên "divGameStage" có tên là "divGameStage", không được lồng trong bất kỳ phần tử nào khác, ngoại trừ body
.Cách thêm CANVAS html5 trong DIV
EDIT: Tôi đã thử các gợi ý đầu tiên, nhưng không có vải đang xuất hiện, mã đầy đủ như sau:
<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>
Bản sao có thể có của [Thêm canvas vào trang có javascript] (http://stackoverflow.com/questions/9152224/add-canvas-to-a-page-with-javascript) – Vadzim