简体   繁体   中英

Having trouble accessing event handler

From what I'm been reading from everywhere I checked, this is how I would access a event handler's attributes:

canvas.addEventListener('onmousemove', traceMouse(e));  //Handler gets passed in as 'e'

function traceMouse(e) {   //Then I could just use it like a normal parameter, right?
    mouseX = e.clientX;
    mouseY = e.clientY;
    }

Unfortunately firebug insists that e is not defined. I have absolutely no idea where I went wrong, I probably made some dumb mistake again, and I'm ready to bite my own head off.

Help?

Actually, in addEventListener , 2nd argument to pass is the name of function which you want to call when event is fired. Now, you are calling the function & passing it e(which is not defined). So, code should be

canvas.addEventListener('mousemove',traceMouse);

And event object will be passed by browser,so you will be able to get clientX & Y.

And in any case you need to pass the argument to function you can use closure. Here is an example. http://jsfiddle.net/parth1403/fVakB/

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