如果是hasClass,那么addClass到parent [英] If hasClass then addClass to parent

查看:111
本文介绍了如果是hasClass,那么addClass到parent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原帖:
为什么这个简单的脚本不起作用?

Original post: Why doesn't this simple script work?

if ($('#navigation > ul > li > ul > li > a').hasClass('.active')) {
    $(this).parent().parent().parent().addClass(".active");
}

编辑:

这不会隐藏H1:

if ($('#content h1').hasClass('aktiv')) {
    $(this).hide();
}

只有这样:

if ($('#content h1').hasClass('aktiv')) {
    $('#content h1').hide();
}

为什么我不能使用(这个)?

Why can't I use the (this)?

推荐答案

点不是类名的一部分。它仅用于CSS / jQuery选择器表示法。请尝试这样做:

The dot is not part of the class name. It's only used in CSS/jQuery selector notation. Try this instead:

if ($('#navigation a').hasClass('active')) {
    $(this).parent().addClass('active');
}

如果 $(this)引用该锚点,你必须将其更改为 $('#navigation a'),因为if条件没有jQuery回调范围。

If $(this) refers to that anchor, you have to change it to $('#navigation a') as well because the if condition does not have jQuery callback scope.

这篇关于如果是hasClass,那么addClass到parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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