Difference between e.target and e.currentTarget

后端 未结 10 909
谎友^
谎友^ 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:56

    e.currentTarget would always return the component onto which the event listener is added.

    On the other hand, e.target can be the component itself or any direct child or grand child or grand-grand-child and so on who received the event. In other words, e.target returns the component which is on top in the Display List hierarchy and must be in the child hierarchy or the component itself.

    One use can be when you have several Image in Canvas and you want to drag Images inside the component but Canvas. You can add a listener on Canvas and in that listener you can write the following code to make sure that Canvas wouldn't get dragged.

    function dragImageOnly(e:MouseEvent):void
    {
        if(e.target==e.currentTarget)
        {
            return;
         }
         else
         {
            Image(e.target).startDrag();
         }
    }
    

提交回复
热议问题