简体   繁体   中英

pass arguements to function called in addEventListener

If I am calling a function in addEventListener like this:

document.querySelector('.elem').addEventListener('click', expand);

The expand function takes arguments. How can I pass those arguments to it?

I've tried

document.querySelector('.elem').addEventListener('click', expand(arg1, arg2, arg3));

But that doesn't work.

With an anonymous function call

document.querySelector('.elem').addEventListener('click', function() {
    expand(arg1, arg2, arg3)
}, false);

if you need the value of this to be the element, you could use bind , apply or call

document.querySelector('.elem').addEventListener('click', function() {
    expand.apply(this, [arg1, arg2, arg3]);
}, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM