How to pass arguments to addEventListener listener function?

前端 未结 30 2294
谎友^
谎友^ 2020-11-21 23:56

The situation is somewhat like-

var someVar = some_other_function();
someObj.addEventListener(\"click\", function(){
    some_function(someVar);
}, false);
<         


        
30条回答
  •  花落未央
    2020-11-22 00:47

        var EV = {
            ev: '',
            fn: '',
            elem: '',
            add: function () {
                this.elem.addEventListener(this.ev, this.fn, false);
            }
        };
    
        function cons() {
            console.log('some what');
        }
    
        EV.ev = 'click';
        EV.fn = cons;
        EV.elem = document.getElementById('body');
        EV.add();
    
    //If you want to add one more listener for load event then simply add this two lines of code:
    
        EV.ev = 'load';
        EV.add();
    

提交回复
热议问题