jQuery Parent()选择器 [英] JQuery Parent() selector

查看:70
本文介绍了jQuery Parent()选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向ul添加一个活动类,下面的代码可以做到这一点,但是有没有一种更好的方法来查找ul而无需使用3个parent()选择器呢?

I want to add a class of active to the ul and this following code does do that, but is there a better way of finding the ul without using 3 parent() selectors?

$('.header ul li img').click(function () {
    $(this).parent().parent().parent().addClass('active');

推荐答案

$.closest()遍历树以查找选择器
文档

$.closest() traverses up the tree looking for a selector
docs

$(this).closest( 'ul' );

这篇关于jQuery Parent()选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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