How to get an element's ID from event.target

前端 未结 5 973
天涯浪人
天涯浪人 2021-01-01 14:26

Consider a piece of code that looks like the following:

$(\'body\').on(\'click\', function(e){

});

I know there is a way to get the elemen

5条回答
  •  不思量自难忘°
    2021-01-01 15:29

    To get the attribute of a target element in JavaScript you would simply use:

    e.target.getAttribute('id');
    

    See also: https://stackoverflow.com/a/10280487/5025060 for the subtle distinction between DOM properties and their attributes.

提交回复
热议问题