javascript - 即使将画布放在脚本之前,也无法读取 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");
解决方案
你的 id 不在 html 标签中
<canvas id="chart" width="720" height="720"></canvas>
应该解决你的问题
推荐阅读
- javascript - 停止计划的推送通知在反应本机推送通知中
- java - Eclipse Java 更新
- python - ValueError:输入 0 与层 bidirectional_1 不兼容:预期 ndim=3,发现 ndim=2
- arrays - 如何在数组中查找元素并仅使用 $project 返回这些元素?
- haskell - 如何在 Haskell 案例中使用“<0”条件
- wordpress - Wordpress REST Api:add_action('rest_api_init', callback) 不调用回调
- laravel - VueJS - 如何将输入参数传递给单页组件
- c# - 在异步 MongoDb 调用中检查存储操作的状态
- powershell - Powershell脚本不会覆盖文件,但会创建一个新文件并覆盖它
- apache-kafka - Confluent Replicator 为同一个集群复制配置模式注册表