Please go through this code.

arrow_left.addEventListener("click",ans_eval);      
arrow_right.addEventListener("click",ans_eval);  //ans_eval is a function.

now if I want to know which object was clicked, how do I find out?

if(e.target.name==arrow_left){         is this the correct way??
} 

How to do it? When I defined arrow_left, I didn't specify any name for that object, so will e.target.name work?

有帮助吗?

解决方案

You don't need the name, it should work fine just like this:

if ( e.target == arrow_left ) { ...

This of course requires that your method ans_eval can access to the arrow_left-variable, meaning that it will be called in the same context and the addEventListener. Wether or not this is the case will only you know or you post the whole code in a js-fiddle.

其他提示

Try giving names to the arrows before adding listeners, like:

arrow_left.name = 'arrow_left';
arrow_right.name = 'arrow_right';

and your function that listens for the mouse click should be like:

if ( e.target == 'arrow_left' ) { ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top