首页 > 解决方案 > 即使将画布放在脚本之前,也无法读取 null 的属性“getContext”

问题描述

我想做的就是开始使用画布,但我继续收到此错误。我见过很多人告诉其他人将画布标签放在脚本标签之前,我已经这样做了,但它并没有解决问题。HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Cross Stats Graph</title>
</head>
<body>
    <canvas> id="chart" width="720" height="720"</canvas>
    <script type="text/javascript" src="sketch.js"></script>
</body>
</html>

Javascript:

var canvas = document.getElementById("chart").getContext("2d");

标签: javascripthtmlcanvas

解决方案


你的 id 不在 html 标签中

<canvas id="chart" width="720" height="720"></canvas>

应该解决你的问题


推荐阅读