Getting the class of the element that fired an event using JQuery

后端 未结 6 1327
独厮守ぢ
独厮守ぢ 2021-01-30 08:04

is there anyway to get the class when click event is fired. My code as below, it only work for id but not class.

6条回答
  •  天涯浪人
    2021-01-30 08:29

    This will contain the full class (which may be multiple space separated classes, if the element has more than one class). In your code it will contain either "konbo" or "kinta":

    event.target.className
    

    You can use jQuery to check for classes by name:

    $(event.target).hasClass('konbo');
    

    and to add or remove them with addClass and removeClass.

提交回复
热议问题