当在 HTML 页面上单击提交按钮时,一个框也应显示在页面上任何颜色的任何位置。我正在使用外部 JavaScript 页面来完成此操作。但是,它不起作用...我尝试对其进行调试,但它不会超过var body = document.getElementsById("body")[0];
这是HTML代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 2 - Question 2</title>
<script src="E02_Q2.js"></script>
</head>
<body>
<form>
<input type="submit" value="Add Box" onclick="ShowBox()"/>
</form>
</body>
</html>
这是伴随它的外部Javascript:
function ShowBox(){
//get the body element of the document
var body = document.getElementsById("body")[0];
//create the canvas tag
var canvas = document.createElement("canvas");
canvas.height = 200;
canvas.width = 200;
var context = canvas.getContext("2d");
//create the box and append onto the canvas
canvas.fillStyle = "#FF0000";
canvas.fillRect(50,50,100,100);
//append the canvas onto the body
body.appendChild(canvas);
}
不太确定我在哪里出错了......