【发布时间】:2015-07-22 19:06:19
【问题描述】:
所以由于某种原因,我的画布在通过 onload 调用时没有被应用,我尝试了其他事件并且它们有效,我尝试更改放置 onload 的位置(页脚、p 和画布标签),但没有'不工作,我通过将它放在一个可以工作的函数的底部来测试脚本功能
我尝试了另一个我做过的网站,它有 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>标签在哪里?? -
@Cheery 我删除了其他所有内容,这只是一个测试页面。
-
@Cheery 问题已解决,我只是在寻找关于为什么 onload 事件不起作用的解释,根据下面的答案,它说它只适用于 body 标签,这也修复了问题。但我想知道为什么它也只适用于 body 标签。
-
不是每个标签都有这个属性和事件 - stackoverflow.com/questions/4272002/… 和
canvas没有它 - w3schools.com/tags/ref_canvas.asp 与body的事件列表比较 - w3schools.com/tags/ref_eventattributes.asp
标签: javascript html canvas