zoukankan      html  css  js  c++  java
  • 事件封装(多个函数绑定一个事件,预计这样解释不正确)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <!--<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />-->
        <title></title>
        <style type="text/css">
            .red{100px;height:100px;background:red;}
            .blue{100px;height:100px;background:blue;}
        </style>
        <script type="text/javascript">
       /*window.onload=function(){
           alert("Lee");
       }
            if(typeof window.onload=="function"){
                var saved=null;
                saved=window.onload;
            }
            window.onload=function(){
            if(saved){
                saved();
            }
             alert("Mr.Lee");
    }*/
       //传统绑定机制
    /*   window.onload=function(){
             var box=document.getElementById("box");


                box.onclick=function() {
                    alert("Lee");
                    toRed().call(this);
                }
            }
            function toRed(){
               // alert(this)
                this.className="red";
                this.onclick=toBlue;
            }
            function toBlue(){
                this.className="blue";
                this.onclick=toRed;
            }


    */
       /* window["onload"]=function(){
        alert("Lee");
        }*/
        /* function addEvent(obj,type,fn) {
             var saved = null;
             if (typeof obj['on' + type] == "function") {
                 saved = obj['on' + type];//保存上一个事件
             }
             //运行事件
             obj['on' + type] = function () {
                 if(saved)saved();//先运行上一个事件
                 fn();
             }
         }
           addEvent(window,"load",function(){
           alert("Lee");
       })
       addEvent(window,"load",function(){
           alert("Leesss");
       })*/

    *****************风格线*********这里才是本章高潮处*******************************************************************


        //当不断点击的时候,浏览器就会卡死,而且报错:too muchrecursion,太多的递归
        //由于积累了太多保存的事件
        //解决方式,就是用完事件就马上清除
       //移除事件
     
     function removeEvent(obj,type){
           if(obj["on"+type]) obj["on"+type]=null;
       }
       //加入事件
       function addEvent(obj,type,fn) {
           var saved = null;
           if (typeof obj['on' + type] == "function") {
               saved = obj['on' + type];//保存上一个事件
           }
           //运行事件
           obj['on' + type] = function () {
               if(saved)saved.call(this);//先运行上一个事件
               fn.call(this);//这两个地方要加个call呢 不然一会this就指向window了
           }
       }


       addEvent(window,'load',function(){
           var box=document.getElementById("box");
          addEvent(box,'click',toRed); //this 没有传递过去
       })
      function toRed(){
           this.className="red";
          removeEvent(this,'click');//移除事件函数
           addEvent(this,'click',toBlue);//加入事件函数
       }
       function toBlue(){
           this.className="blue";
           removeEvent(this,'click');//移除事件函数
           addEvent(this,'click',toRed);//加入事件函数
       }




        </script>
    </head>
    <body>
    <div id="box" class="blue">測算点</div>
    </body>

    </html>


  • 相关阅读:
    HDU-1421 搬寝室(递推,DP,数学)2017寒假集训
    HDU-2577 How to Type(递推,DP)(水)2017寒假集训
    POJ-1088 滑雪(DP,二维LIS,记忆化)2017寒假集训
    CSUST-17级集训队选拔赛解题报告
    (十六)服务化:微服务架构,必须搞定高可用!
    (十五)服务化:微服务架构,粒度多少合适?
    (十四)服务化:微服务架构,究竟解决什么问题?
    (十三)本章小结:百万流量,这些技术够用了
    (十二)性能优化:读写分离,前台与后台分离
    (十一)性能优化:动静分离,互联网优化利器?
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3913267.html
Copyright © 2011-2022 走看看