[removed] Find out which element was clicked witho

2020-03-02 03:13发布

I am confused on finding an approach to resolve this issue. Consider below html

<body>
  <div id="parent" onclick="myFunc">
     <div id="child-a"></div>
     <div id="child-b"></div>
     <div id="child-c"></div>
     <div id="child-d"></div>
     <div id="child-e"></div>  
  </div>
</body>

Event listener is attached to the parent element. If a user clicks on lets say 'child-c', is there any way to find out using "myFunc" that which div was clicked? In this case 'child-c'. Any possible solution using pure JS or jQuery?

Let me know if more explanation is required. Thank you for helping.

-Nishant

3条回答
神经病院院长
2楼-- · 2020-03-02 03:27
$("#parent").click(function(e){
   console.log(e.target);
});

Demo: http://jsfiddle.net/4bC6K/

查看更多
Fickle 薄情
3楼-- · 2020-03-02 03:42

Yes, use target:

function(e){
    // e.target is the clicked div
}

Example: http://jsfiddle.net/Paulpro/AfA4t/

查看更多
我想做一个坏孩纸
4楼-- · 2020-03-02 03:42

Yes, you can check the event's target property which will indicate the element that was clicked. You can read more about event properties here.

查看更多
登录 后发表回答