How to get the class of the clicked element?

后端 未结 6 1848
抹茶落季
抹茶落季 2020-11-29 16:34

I can\'t figure it out how to get the class value of the clicked element.

When I use the code bellow, I get \"node-205\" every time

6条回答
  •  长情又很酷
    2020-11-29 17:11

    All the solutions provided force you to know the element you will click beforehand. If you want to get the class from any element clicked you can use:

    $(document).on('click', function(e) {
        clicked_id = e.target.id;
        clicked_class = $('#' + e.target.id).attr('class');
        // do stuff with ids and classes 
        })
    

提交回复
热议问题