简体   繁体   中英

Attach event using Javascript

I'd like to attach an event when a function is called, but it seems not working using addEventListener ...

My code :

function add () {
  this.inputString.input.addEventListener('mouseover', toggle.call(this));
}

function toggle () {
  $(this.tooltip).toggle();
}

Issue : the toggle() function doesn't work. However, this.inputString.input and this.tooltip are not emtpy...

The apparent problem I see here is that you should attach a function to addEventListener 's second argument, not the call itself:

this.inputString.input.addEventListener('mouseover', toggle);

Then you should consider reviewing the toggle() function itself.
What do you mean by this.tooltip ?

But this is out of scope of this question (about attaching events).

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