gpt4 book ai didi

javascript - 类型错误 : document. getElementById(...) 为空

转载 作者:行者123 更新时间:2023-11-30 09:48:29 25 4
gpt4 key购买 nike

<html>
<head>
</head>
<body>
<h2>Example of Line Graph</h2>
<script src="../../../Scripts/Chart.min.js"></script>
<script src="../../../Scripts/Chart.js"></script>
<script>
var monthsData = {
labels: ["January", "February", "March", "April", "May", "June"],
datasets: [
{
fillColor: "rgba(172,194,132,0.4)",
strokeColor: "#ACC26D",
pointColor: "#fff",
pointStrokeColor: "#9DB86D",
data: [203, 156, 99, 251, 305, 247]
}
]
};
var months = document.getElementById("chartData").getContext("2d");
new Chart(months).Line(monthsData);
</script>
<div>
<canvas id="chartData" width="600" height="400"></canvas>
</div>
</body>
</html>

我已经看到关于同一问题的先前错误并纠正了所有这些错误,但我的错误没有得到解决。我正在使用 chart.js 库。

最佳答案

将您的 script 标记放在包含 canvas 元素的 div 下方。

您收到错误是因为在解释和执行脚本时,DOM 中不存在 Canvas 。

MDN page about script tags指出:

Scripts without async or defer attributes, as well as inline scripts, are fetched and executed immediately, before the browser continues to parse the page.

话虽这么说,我的回答提出了错误的原因和一个简单的解决方法。查看Satpal's answer以获得更惯用的解决方案。

关于javascript - 类型错误 : document. getElementById(...) 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com