为什么window.onload起作用并且onload =””不起作用 [英] Why window.onload works and onload="" does not work

查看:369
本文介绍了为什么window.onload起作用并且onload =””不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,在通过onload调用时未应用我的canvas,我尝试了其他事件并且它们起作用了,我试图更改onload的放置位置(页脚,p和canvas标记),但这并没有't不起作用,我通过将脚本放置在可以正常工作的功能的底部来测试了脚本功能

So for some reason my canvas wasn't being applied when called through onload, I've tried other events and they work, I've tried changing where onload is placed (the footer, p and canvas tags) but that didn't work, and i tested the script functionality by placing it at the bottom outside of a function which does work

我尝试过另一个网站,该网站具有onload和canvas ',并且它们起作用了,因此在查找了可能的解决方案之后,我最终不得不在脚本区域中添加windows.onload并对其进行了修复

Ive tried another website which I did that had onload and canvas' and they work so after looking up possible solutions I ended up having to add windows.onload in the script area and it fixed it

我想知道的是为什么标记中的onload不起作用

What I would like to know is why doesn't the onload in the tags work

<!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()事件。

This is because only the body element and img element can fire the onload() event.

在脚本标记内使用函数名称 draw(),例如< script>画(); < / script> 在画布标签之后。

Just simply use the function name draw() inside the script tag like <script> draw(); </script> after the canvas tag..

这篇关于为什么window.onload起作用并且onload =””不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆