Canvas onload事件未触发 [英] Canvas onload event isn't firing

查看:1652
本文介绍了Canvas onload事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能得到 onload 事件为 Canvas 元素工作。这里是HTML:

I can't get the onload event to work for a Canvas element. Here's the HTML:

<!DOCTYPE html>
<html>
    <head>
        <script src="js/main.js"></script>
    </head>
    <body>
        <canvas id="modelZone" onload="initializeCanvas();" width="400" height="300"></canvas>
    </body>
</html>

下面是 main.js

function initializeCanvas(){
    alert("hello");
}

我知道HTML找到 main.js ,因为如果我将 onload 更改为 onclick ,它可以正常工作:画布触发警报。那么为什么我不能用 onload

I know that the HTML is finding main.js, because if I change onload to onclick it works correctly: clicking in the region of the Canvas triggers an alert. So why can't I do this with onload?

推荐答案

body元素(也许iframe)可以触发一个onload事件。只需将< script> initializeCanvas(); < canvas> 标签之后的< / script>

Only the body element (and perhaps iframe) can fire a onload event. Simply put <script> initializeCanvas(); </script> after the <canvas> tag.

这篇关于Canvas onload事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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