获取使用JQuery触发事件的元素的类 [英] Getting the class of the element that fired an event using JQuery

查看:77
本文介绍了获取使用JQuery触发事件的元素的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何都会在点击事件被触发时获取该类。我的代码如下,它只适用于id但不适用于类。

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

$(document).ready(function() {
  $("a").click(function(event) {
    alert(event.target.id + " and " + event.target.class);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  <a href="#" id="kana1" class="konbo">click me 1</a>
  <a href="#" id="kana2" class="kinta">click me 2</a>
</body>

</html>

jsfiddle code 这里

jsfiddle code here

推荐答案

尝试:

$(document).ready(function() {
    $("a").click(function(event) {
       alert(event.target.id+" and "+$(event.target).attr('class'));
    });
});

这篇关于获取使用JQuery触发事件的元素的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆