因此,由于某种原因,通过 onload 调用时我的 Canvas 没有被应用,我尝试了其他事件并且它们有效,我尝试更改 onload 的放置位置(页脚、p 和 canvas 标签),但这并没有不起作用,我通过将其放置在起作用的函数之外的底部来测试脚本功能
我尝试过另一个网站,它有 onload 和 canvas',它们工作正常,所以在查找可能的解决方案后,我最终不得不在脚本区域添加 windows.onload 并修复了它
我想知道的是为什么标签中的onload不起作用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script>
function draw(){
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(17,17,15,0,2*Math.PI);
ctx.closePath();
ctx.lineWidth = 2;
ctx.fillStyle = 'yellow';
ctx.fill();
ctx.stroke();
}
window.onload= draw;
</script>
</head>
<footer>
<p>
<canvas id="canvas" width="34" height="34" onload="draw()"></canvas>
</p>
</footer>
</html>
最佳答案
这是因为只有 body 元素和 img
元素可以触发 onload()
事件。
只需使用函数名称 draw()
在脚本标签内,如 <script> draw(); </script>
在 Canvas 标签之后..
关于javascript - 为什么 window.onload 有效而 onload =""不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140026/