onload和onclick及其他事件的默认执行顺序

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
        <!-- 以ie最新模式渲染当前页面 -->
        <!--  X-UA-Compatible是ie8专有meta属性-->
        <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
        <title>document.wirte()测试</title>

    </head>
    <body>
        <div class="wrap" style="text-align:center;">
            <button id="btn" >千万不要点,如果点了这个页面所有内容都被覆盖掉了</button>
        </div>

       <script>
       /*  onclick事件在onload事件后执行,执行结果成功 */
       window.onload = function(){
            alert("页面加载完成了,后续onclick事件开始执行");
            document.getElementById("btn").onclick=function()
            {   
                alert("onclick事件开始执行");
                document.write("<center style='color:red;'>让你不要点不要点,你非要点,看吧,是不是页面之前的所有内容都没了</center><br/>");
                document.write("<center style='color:red;'>为什么页面之前的内容没有了?</center><br/>");
                document.write("<center style='color:red;'>答:因为页面加载完成以后,浏览器输出流随之关闭。给button添加了onclick事件,当该事件被触发时,浏览器输出流重新打开,重新输出内容,所以之前的内容会被覆盖掉</center>");
                // 是否说明onclick事件以及其他事件会触发或者说促使页面加载完成,即页面onload事件的触发?
                //document.close();
            }
        }

        /* onload事件在onclick之后执行,执行结果 失败 

        document.getELementById("btn").onclick = function(){
            alert("onclick事件开始执行");
            window.onload = function(){
                alert("onload事件开始执行");
                document.write("<center style='color:red;'>让你不要点不要点,你非要点,看吧,是不是页面之前的所有内容都没了</center><br/>");
                document.write("<center style='color:red;'>为什么页面之前的内容没有了?</center><br/>");
                document.write("<center style='color:red;'>答:因为页面加载完成以后,浏览器输出流随之关闭。给button添加了onclick事件,当该事件被触发时,浏览器输出流重新打开,重新输出内容,所以之前的内容会被覆盖掉</center>");
            }
        }

        */
        </script>
    </body>
</html>
总结:说明onclick及其他事件都在window.onload之后执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值