美文网首页
jquery 的 on 和 off 的简单实现

jquery 的 on 和 off 的简单实现

作者: D_R_M | 来源:发表于2019-04-21 13:01 被阅读0次
<!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">
    <link rel="stylesheet" href="./css/main.css">
    <title>jQuery:on和off的实现</title>
</head>
<body>
    <button id="b1">btn1</button>
    <hr/>
    <div class="time">div3</div>
    <script>
    ;(function(win,dom){
        var jQuery = function(nodeStr){
            if(!(this instanceof jQuery)) return new jQuery(nodeStr);
            this.node = dom.querySelector(nodeStr);
            this.nodeName = nodeStr;
        }
        jQuery.prototype.EventBox = {};
        jQuery.prototype.on = function(eventString,fun){
            if(typeof this.EventBox[this.nodeName]!='undefined'){
                this.EventBox[this.nodeName][eventString] = fun;
            }else{
                this.EventBox[this.nodeName] = {[eventString]:fun};
            }
            this.node.addEventListener(eventString,this.EventBox[this.nodeName][eventString]);
        } 
        jQuery.prototype.off = function(eventString){
            if(typeof this.EventBox[this.nodeName]!='undefined'){
                this.node.removeEventListener(eventString,this.EventBox[this.nodeName][eventString]);
            }
        }           
        win.$=jQuery;
    })(window,document)
    $('#b1').on('click',function(){
        console.log('绑定成功!')
    })
    var count = 4;
    (function time(){
        $('.time').node.innerHTML = count+'秒后移除事件';
        if(count-- != 0){
            setTimeout(time,1000)
        }else{
            $('.time').node.innerHTML = '移除事件.';
            $('#b1').off('click');
        }
    })()
    </script>
</body>
</html>

相关文章

网友评论

      本文标题:jquery 的 on 和 off 的简单实现

      本文链接:https://www.haomeiwen.com/subject/dlepwqtx.html