如何找到被点击元素的父类?
目前在名为click
.cross
事件上运行此事件
我想找到父类名:
var status = $(this).parent(this).hasClass();
console.log(status);
上面的代码在控制台中输出false
而不是类名。
答案 0 :(得分:2)
如何找到被点击元素的父类?
$(this).parent().attr("class")
答案 1 :(得分:1)
为什么您的代码不起作用
jquery中的 .hasClass()
用于查找特定元素中是否存在类。所以它总是返回布尔值true或false。
您的代码
在你的问题中,你想获得Parent类,所以在jquery中使用.attr()
。类也是元素
var status = $(this).parent().attr('class');