zoukankan      html  css  js  c++  java
  • 自定义事件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <div id="div1">div</div>
    </body>
    <script>
        var oDiv = document.getElementById('div1');
    
        bindEvent(oDiv,'click',function(){
            alert(1);
        });
        bindEvent(oDiv,'click',function(){
            alert(2);
        });
        bindEvent(oDiv,'click',function(){
            alert(3);
        });
        bindEvent(oDiv,'show',function(){
            alert(4);
        });
        bindEvent(oDiv,'show',function(){
            alert(5);
        });
        bindEvent(document,'show',function(){
            alert(5);
        });
    
        fireEvent(oDiv,'show');
        function bindEvent(obj,event,fn){
            obj.listeners = obj.listeners || {};
            obj.listeners[event] = obj.listeners[event] || [];
            obj.listeners[event].push(fn);
            obj.addEventListener(event,fn,false);
        }
    
        function fireEvent(obj,event){
            var fn = obj.listeners[event];
            console.log(fn);
            for(var i=0;i<fn.length;i++){
                obj.listeners[event][i]();
            }
        }
    
    </script>
    </html>
  • 相关阅读:
    微信小程序
    svn
    当滑动条滑动到某一位置触发js
    css固定页面
    css三级菜单
    h5时钟
    DOM节点
    应用r.js来优化你的前端
    浅谈javascript中的作用域
    javascript 中的 arguments,callee.caller,apply,call 区别
  • 原文地址:https://www.cnblogs.com/chenzhiyu/p/8872391.html
Copyright © 2011-2022 走看看