How to pass arguments to addEventListener listener function?

前端 未结 30 2266
谎友^
谎友^ 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:57

    Also try these (IE8 + Chrome. I dont know for FF):

    function addEvent(obj, type, fn) {
        eval('obj.on'+type+'=fn');
    }
    
    function removeEvent(obj, type) {
        eval('obj.on'+type+'=null');
    }
    
    // Use :
    
    function someFunction (someArg) {alert(someArg);}
    
    var object=document.getElementById('somObject_id') ;
    var someArg="Hi there !";
    var func=function(){someFunction (someArg)};
    
    // mouseover is inactive
    addEvent (object, 'mouseover', func);
    // mouseover is now active
    addEvent (object, 'mouseover');
    // mouseover is inactive
    

    Hope there is no typos :-)

提交回复
热议问题