Difference between e.target and e.currentTarget

后端 未结 10 906
谎友^
谎友^ 2020-11-22 06:29

I don\'t understand the difference, they both seem the same but I guess they are not.

Any examples of when to use one or the other would be appreciated.

10条回答
  •  醉话见心
    2020-11-22 06:50

    Ben is completely correct in his answer - so keep what he says in mind. What I'm about to tell you isn't a full explanation, but it's a very easy way to remember how e.target, e.currentTarget work in relation to mouse events and the display list:

    e.target = The thing under the mouse (as ben says... the thing that triggers the event). e.currentTarget = The thing before the dot... (see below)

    So if you have 10 buttons inside a clip with an instance name of "btns" and you do:

    btns.addEventListener(MouseEvent.MOUSE_OVER, onOver);
    // btns = the thing before the dot of an addEventListener call
    function onOver(e:MouseEvent):void{
      trace(e.target.name, e.currentTarget.name);
    }
    

    e.target will be one of the 10 buttons and e.currentTarget will always be the "btns" clip.

    It's worth noting that if you changed the MouseEvent to a ROLL_OVER or set the property btns.mouseChildren to false, e.target and e.currentTarget will both always be "btns".

提交回复
热议问题