var classes = $(this).attr('class').split(' '); // this gets the current element classes

var classes = $(this).parent().attr('class').split(' '); // this gets the parent classes.

在上述情况下,父母是羚羊.

If I wanted to get the first parent DIV of $(this) what would the code look like?

var classes = $(this).div:parent().attr('class').split(' '); // just a quick try.

* Basically I want to get the classes of the first parent DIV of $(this).

谢谢

推荐答案

Use .closest() to traverse up the DOM tree up to the specified selector.

var classes = $(this).parent().closest('div').attr('class').split(' '); // this gets the parent classes.

Jquery相关问答推荐

jQuery:在mousemove事件期间检测按下的鼠标按钮

如何使用 jquery 获取屏幕的高度

在 JSON 对象上使用 jQuery 的 find()

在 div 中找到第一次出现的类

如何使用 jQuery 获取所有 ID?

如何在数据表中显示空数据消息

jQuery:获取所选单选按钮的父 tr

JQuery 数据 Select 器不使用 .data 更新

jQuery 的 ajax 默认超时值是多少?

为什么要两次声明 jQuery?

Chart.js 中饼图的点击事件

jQuery-UI 的自动完成不能很好地显示,z-index 问题

jQuery:获取父母,父母ID?

如何使用 jquery 动态更改 iframe 中的内容?

如何使用jquery获取点击链接的href?

使用 JQuery 的黄色淡入淡出效果

Jquery Select 器输入[type=text]')

JS - 从base64代码中获取图片的宽高

如何在 jquery 中获取 textarea 的值?

小于 10 给数字加 0