Wrong target when pressing html button with html tags in it

前端 未结 1 1631
半阙折子戏
半阙折子戏 2020-12-12 07:15

I have four buttons and want to trace which one is pressed. In some of them I changed innerHTML property with HTML tag. Then I noticed that when a button is pressed there is

相关标签:
1条回答
  • 2020-12-12 07:44

    Use currentTarget instead. From MDN:

    It always refers to the element the event handler has been attached to as opposed to event.target which identifies the element on which the event occurred

    function clickHandler(me){
        console.log(me.currentTarget);
    }
    

    Here is a fiddle to demonstrate the above.

    0 讨论(0)
提交回复
热议问题