• js以类似jquery的模式绑定事件


    <!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>
        <button class="a"></button>
    </body>
    
    </html>
    <script>
        var $ = function (el) {
            return new _$(el);
        };
        var _$ = function (el) {
            this.el = (el && el.nodeType == 1) ? el : document;
        };
        _$.prototype = {
            constructor: _$,
            addEvent: function (type, fn, capture) {
                var el = this.el;
    
                if (window.addEventListener) {
                    el.addEventListener(type, fn, capture);
    
                    var ev = document.createEvent("HTMLEvents");
                    ev.initEvent(type, capture || false, false);
                    // 在元素上存储创建的事件,方便自定义触发
                    if (!el["ev" + type]) {
                        el["ev" + type] = ev;
                    }
    
                } else if (window.attachEvent) {
                    el.attachEvent("on" + type, fn);
                    if (isNaN(el["cu" + type])) {
                        // 自定义属性,触发事件用
                        el["cu" + type] = 0;
                    }
    
                    var fnEv = function (event) {
                        if (event.propertyName == "cu" + type) {
                            fn.call(el);
                        }
                    };
    
                    el.attachEvent("onpropertychange", fnEv);
    
                    // 在元素上存储绑定的propertychange事件,方便删除
                    if (!el["ev" + type]) {
                        el["ev" + type] = [fnEv];
                    } else {
                        el["ev" + type].push(fnEv);
                    }
                }
    
                return this;
            },
            fireEvent: function (type) {
                var el = this.el;
                if (typeof type === "string") {
                    if (document.dispatchEvent) {
                        if (el["ev" + type]) {
                            el.dispatchEvent(el["ev" + type]);
                        }
                    } else if (document.attachEvent) {
                        // 改变对应自定义属性,触发自定义事件
                        el["cu" + type]++;
                    }
                }
                return this;
            },
            removeEvent: function (type, fn, capture) {
                var el = this.el;
                if (window.removeEventListener) {
                    el.removeEventListener(type, fn, capture || false);
                } else if (document.attachEvent) {
                    el.detachEvent("on" + type, fn);
                    var arrEv = el["ev" + type];
                    if (arrEv instanceof Array) {
                        for (var i = 0; i < arrEv.length; i += 1) {
                            // 删除该方法名下所有绑定的propertychange事件
                            el.detachEvent("onpropertychange", arrEv[i]);
                        }
                    }
                }
                return this;
            }
        };
    
        var dom = document.querySelector(".a");
        var test = $(dom).addEvent("alert", function () {
            alert("弹弹弹,弹走鱼尾纹~~");
        }, false);
    
    test.fireEvent("alert");
    </script>
  • 相关阅读:
    switch的使用
    ArrayAdapter的使用
    android的xml中怎么实现按钮按下去变颜色
    Intent跳转的设置和Bundle的使用
    监听JList列表项的单击事件
    草稿
    Android背景图覆盖状态栏(我的手机安卓版本是4.2.2)
    RSA加密解密 (输入数值)
    仿射密码加密解密 (输入字母数值)
    Intent.ACTION_PICK
  • 原文地址:https://www.cnblogs.com/gaocong/p/7849932.html
Copyright © 2020-2023  润新知