Canvas onload事件未触发

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.