简体   繁体   English

Jquery如果是hasClass然后是addClass

[英]Jquery if hasClass then addClass

I know I can do this, I'm just getting lost in the hierarchy and need a second set of eyes on this. 我知道我可以做到这一点,我只是迷失在层次结构中,需要第二眼看到这一点。

Here's the structure 'm working with: 这是我正在使用的结构:

<div class="nav-column">
    <ul>
        <li><a href="#">Link 01</a>
            <div>
                <ul>
                    <li><a href="#">Sublink 01</a></li>
                    <li><a href="#">Sublink 02</a></li>
                    <li><a href="#">Sublink 03</a></li>
                </ul>
            </div>
        </li>
        <li><a href="#">Link 02</a></li>
        <li><a href="#">Link 03</a></li>
    </ul>
    <div class="home"><h3>Underlying Div</h3></div>
</div>

I am looking to do the following: when you hover over a .nav-column ul li a that visibility of div.home would turn off. 我希望执行以下操作:当您将鼠标悬停在.nav-column ul li a上时, div.home可见性将会关闭。 Of course there are going to be multiple .nav-columns so I'm making this dynamic. 当然会有多个.nav-columns,所以我让它变得动态。

The jQuery I have right now is: 我现在的jQuery是:

if ($('.nav-column li').hasClass('active')){
     $(this).parent('.nav-column').sibling('div.home').toggleClass("off");
}

without yielding any class addition to the div.home. 没有给div.home添加任何类。 I already have a hover function adding and removing the class '.active' to the .nav-column li 我已经有了一个悬停功能,可以将类'.active'.nav-column li


EDIT EDIT EDIT 编辑编辑


I see that I have made a mistake with my code, and in fact the correct code has the div.home OUTSIDE the div.nav-column 我看到我的代码犯了一个错误,事实上正确的代码有div.nav-column div.homediv.nav-column

This is the proper heirarchy: 这是正确的层次结构:

<div class="wrapper">
    <div class="nav-column">
        <ul>
            <li><a href="#">Link 01</a>
                <div>
                    <ul>
                        <li><a href="#">Sublink 01</a></li>
                        <li><a href="#">Sublink 02</a></li>
                        <li><a href="#">Sublink 03</a></li>
                    </ul>
                </div>
            </li>
            <li><a href="#">Link 02</a></li>
            <li><a href="#">Link 03</a></li>
        </ul>
    </div>
    <div class="home"><h3>Underlying Div</h3></div>
</div>

Once again... I am very sorry... you can sense my sanity levels dropping 再一次......我很抱歉......你能感觉到我的理智水平在下降

Think this is what you want: 认为这是你想要的:

$('.nav-column li').each(function(){
    if($(this).hasClass('active')) {
        $(this).closest('.nav-column').siblings('div.home').toggleClass("off");
    } 
});

Fiddle: 小提琴:

http://jsfiddle.net/Jf8mp/ http://jsfiddle.net/Jf8mp/

Your mistakes: 你的错误:

.sibling('div.home') is wrong, the correct name of method is .siblings() .sibling('div.home')错误,方法的正确名称是.siblings()

if condition doesnt determine who is $(this) , you have use a function as .each() 如果条件不确定谁是$(this) ,则使用函数为.each()

UPDATED: 更新:

to make it work on hover over .nav-column ul li a : 使其悬停在.nav-column ul li a

$('.nav-column li').on('mouseenter','a',function(){
    if($(this).closest('li').hasClass('active')) {
        $(this).closest('.nav-column').siblings('div.home').toggleClass("off");
    } 
});

Fiddle: 小提琴:

http://jsfiddle.net/Jf8mp/2/ http://jsfiddle.net/Jf8mp/2/

You need to use .parents() instead of .parent() . 您需要使用.parents()而不是.parent() .parents() traverses up multiple levels of the DOM while .parent() retrieves the immediate parent of the element matching the selector. .parents()遍历DOM的多个级别,而.parent()检索与选择器匹配的元素的直接父级。

$(this).parents('.nav-column').siblings('div.home').toggleClass("off");

Since you're not targetting the direct parent ( ul ) of the element matching the selector, you'll need to use .parents() instead. 由于您没有定位与选择器匹配的元素的直接父( ul ),因此您需要使用.parents()

In addition, you have a second problem. 另外,你有第二个问题。 According to your code structure div.home is not a sibling of .nav-column . 根据你的代码结构, div.home不是 .nav-column的兄弟。 You can use .find() for this instead. 您可以使用 .find()代替。

Per your last edit, the previous statement is no longer applicable. 根据您的上次编辑,之前的声明不再适用。 The code snippets have been updated to reflect your edited change. 代码段已更新,以反映您编辑的更改。

Alternatively, you could do the following to accomplish the same effect: 或者,您可以执行以下操作以实现相同的效果:

$(this).parents('.nav-column').next().toggleClass("off");

use .closest() or .parents() instead of .parent() . 使用.closest().parents()代替.parent() also div home is not sibling of .nav-column. div家也不是.nav-column的兄弟姐妹。 You need to use .find() instead of .siblings() .Try this: 你需要使用.find()而不是.siblings().siblings()这个:

 $(this).closest('.nav-column').find('div.home').toggleClass("off");

or 要么

 $(this).parents('.nav-column').find('div.home').toggleClass("off");

.parent()只上一个节点,你需要使用.parents('select')

If I understand: 如果我明白:

$('.nav-column li').forEach(elem, index){
    elem = $(elem);
    if (elem.hasClass('active')){
       elem.addClass("Cheese");
    }
}

This would add the class "Cheese" to any active li :) 这会将类“Cheese”添加到任何活跃的li :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM