I would like help with this problem, i have a file named, index.html, and a file named javascript.js. When i do document.getElementById("canvas"), and then try to change something like the width of the canvas, it gives me a null exception.
Html file
<html>
<body>
<canvas> </canvas>
<script src="javascript.js"></script>
<script>getCanvas.start();</script>
</body>
</html>
JavaScript file
var canvas;
function getCanvas() {
canvas = document.getElementById("canvas");
canvas.width = 500;
}
At line 4, google chrome throws me the following error on the console. Uncaught TypeError: Cannot set the property 'width' of null.
A huge thanks to anybody who helps.
您必须为画布元素指定id
<canvas id="canvas"> </canvas>
document.getElementById("canvas");
will get you the element which has the id of 'canvas'. Your canvas element does not have the id 'canvas'.
Solution :
<canvas id="canvas"> </canvas>
document.getElementById("canvas");
This it self say get Element By ID and you didn't give set id.
Try This in your HTML:-
<canvas id="canvas"> </canvas>
在不更改HTML的情况下,您可以这样使用它:
document.body.getElementsByTagName('canvas');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.