JavaScript中window.onload和document.onload有什么区别?

JavaScript中window.onload和document.onload有什么区别?

document.onload

在加载图像和其他外部内容之前将其触发。文件。在window.onload之前触发onload事件。

window.onload

加载整个页面(包括图像,脚本,css等)时,它将被触发。

示例

这是一个了解onload的示例。

 

<html>
   <head>
      <title>JavaScript Animation</title>
      <script>
         <!--
            var imgObj = null;
         
            function init() {
               imgObj = document.getElementById('myImage');
               imgObj.style.position= 'relative';
               imgObj.style.left = '0px';
            }

            function moveRight() {
               imgObj.style.left = parseInt(imgObj.style.left) + 10 + 'px';
            }
            window.onload =init;
         //-->
      </script>
   </head>

   <body>
      <form>
         <img id="myImage" src="/images/html.gif" />
         <p>Click button below to move the image to right</p>
         <input type="button" value="Click Me" onclick="moveRight();" />
      </form>
   </body>
</html>
 
 

posted on 2022-10-29 19:51  漫思  阅读(46)  评论(0编辑  收藏  举报

导航