zoukankan      html  css  js  c++  java
  • [读码时间] 事件练习:封闭兼容性添加,删除事件的函数

    说明:代码取自网络,注释为笔者学习时添加!

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>事件练习:封闭兼容性添加,删除事件的函数</title>
        <style>
            pre{
                color:green;
                padding:10px 15px;
                background:#f0f0f0;/*白色*/
                border:1px solid #333;/*黑色*/
                font:12px/1.5 Courier New;/*行高18*/
            }
            span{
                color:#999;/*灰黑色*/
            }
        </style>
        <script>
    var EventUtil = {
        addHandler: function (oElement, sEvent, fnHandler) {
            oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)
        },
        removeHandler: function (oElement, sEvent, fnHandler) {
            oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
        },
        addLoadHandler: function (fnHandler) {
            this.addHandler(window, "load", fnHandler)
        }
    };
    EventUtil.addLoadHandler(function () {
        var aBtn = document.getElementsByTagName("input");
    
        //为第一个按钮添加绑定事件
        EventUtil.addHandler(aBtn[1], "click", function () {
            EventUtil.addHandler(aBtn[0], "click", fnHandler);
            aBtn[0].value = "我可以点击了"
        });
    
        //解除第一个按钮的绑定事件
        EventUtil.addHandler(aBtn[2], "click", function () {
            EventUtil.removeHandler(aBtn[0], "click", fnHandler);
            aBtn[0].value = "毫无用处的按钮"
        });
    
        //事件处理函数
        function fnHandler ()
        {
            alert("事件绑定成功!")
        }
    })
        </script>
    </head>
    <body>
        <pre>
    &lt;script type="text/javascript"&gt;
    var EventUtil = {
        addHandler: function (oElement, sEvent, fnHandler) {
            oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)    
        },
        removeHandler: function (oElement, sEvent, fnHandler) {
            oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
        },
        addLoadHandler: function (fnHandler) {
            this.addHandler(window, "load", fnHandler)
        }
    }
    &lt;/script&gt;
    </pre>
        <center><input type="button" value="毫无用处的按钮"> <input type="button" value="绑定click"> <input type="button" value="解除绑定"></center>
    </body>
    </html>
    View Code
  • 相关阅读:
    MPI消息传递MPI_Sendrecv的用法
    外网SSH访问内网LINUX服务器
    LINUX下Doxygen的配置与使用
    C语言中关键字const一般的用途
    Ubuntu使用apt-get时提示>”E: You must put some ‘source’ URIs in your sources.list”
    C语言中复数运算及调用blas,lapack中复数函数进行科学计算
    linux系统下C语言调用lapack ,blas库
    一封家书,道尽顶尖人才的思维境界
    学会用麦肯锡的方式思考
    记得自己的大梦想
  • 原文地址:https://www.cnblogs.com/sx00xs/p/6487780.html
Copyright © 2011-2022 走看看