使用 .parent 和 .hasClass 查找父 class

Finding parent class using .parent and .hasClass

如何找到点击元素的父元素 class?

目前 运行 这个发生在名为 .cross

的元素的 .click 事件上

我想找到父class姓名:

var status = $(this).parent(this).hasClass();
        console.log(status);

上面的代码在控制台中输出 false 而不是 classname。

为什么你的代码不起作用

.hasClass() in jquery 用于查找特定元素中是否存在 class。所以它总是 return 布尔值 true 或 false 。

你的代码是

在你的问题中,你想得到父 class,所以在 jquery 中使用 .attr()。 Class也是元素中的一种属性

var status = $(this).parent().attr('class');

How can I find the parent class of a clicked element?

$(this).parent().attr("class")