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

后端 未结 6 1318
独厮守ぢ
独厮守ぢ 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:47

    $(document).ready(function() {
      $("a").click(function(event) {
        var myClass = $(this).attr("class");
        var myId = $(this).attr('id');
        alert(myClass + " " + myId);
      });
    })
    
    
    
      
    
    
    
      click me 1
      click me 2
    
    
    

    This works for me. There is no event.target.class function in jQuery.

提交回复
热议问题