search

Home  >  Q&A  >  body text

javascript - There is an img tag in a div. Why do I bind events to the div and when printing the clicked object, sometimes it is the img object instead of the div object?

When the picture is clicked, the function is also triggered, but I did not bind it to the img. .

迷茫迷茫2813 days ago699

reply all(4)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-12 09:31:01

    Use event.currentTarget, event.target returns the element that triggered the event, and event.currentTarget returns the element that bound the event

    reply
    0
  • 世界只因有你

    世界只因有你2017-06-12 09:31:01

    Please show how the chooseIcon() method is written.

    reply
    0
  • 黄舟

    黄舟2017-06-12 09:31:01

    It should be img in p. You can click on the picture to trigger the event. You can print it and it will come out

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-12 09:31:01

    Because there is an event bubbling mechanism, when you click img, the binding event of the parent p will also be triggered.

    reply
    0
  • Cancelreply