How to pass arguments to addEventListener listener function?

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

    In 2019, lots of api changes, the best answer no longer works, without fix bug.

    share some working code.

    Inspired by all above answer.

     button_element = document.getElementById('your-button')
    
     button_element.setAttribute('your-parameter-name',your-parameter-value);
    
     button_element.addEventListener('click', your_function);
    
    
     function your_function(event)
       {
          //when click print the parameter value 
          console.log(event.currentTarget.attributes.your-parameter-name.value;)
       }
    

提交回复
热议问题