简体   繁体   English

jQuery菜单触发内容

[英]JQuery menu trigger content

I have a menu with a bunch of list items. 我有一个带有一堆列表项的菜单。 What I am trying to do is toggle a "highlight" class for which ever one the use clicks on it will change its properties. 我想做的是切换一个“高亮”类,使用该类的人只要点击它,就会改变其属性。

Now the problem I am having is when the user clicks on one and makes it the active one when he/she clicks on the active one again it reverts back to its original state but changes all of the other list items to the "highlight" class properties. 现在,我遇到的问题是,当用户单击一个并使其变为活动状态时,他/她再次单击该活动状态,它又恢复到其原始状态,但是将所有其他列表项更改为“突出显示”类属性。 I would like to make it so that it doesnt change the properties of the active one already and doesnt change the other list items either unless of course they want to click on the other list item then that previous active one reverts back and the new one changes. 我要这样做,以便它既不会更改活动列表的属性,也不会更改其他列表项,除非他们当然想单击其他列表项,然后使先前的活动列表恢复原状,而新的活动列表会更改。 Code below. 下面的代码。

    .highlight { background:black; color:white }

<ul id="momslist">
    <li><a href="#"><img src="#" alt=""/></a>
    <p>Name 4</p></li>
    <li><a href="#"><img src="#" alt=""/></a>
    <p>Name 3</p></li>
    <li><a href="#"><img src="#" alt=""/></a>
    <p>Name 2</p></li>
    <li><a href="#"><img src="#" alt=""/></a>
    <p>Name 1</p></li>
</ul>


$('#momslist li a').click(function() {
    var parent = $(this).parent();
    siblings = parent.siblings(),
    isOn = parent.toggleClass('highlight').hasClass('highlight');

    siblings.toggleClass('highlight', !isOn);
});

The problem is your !isOn - that's going to do the opposite - ie turn everything on when you turn that one off. 问题是您的!isOn-会做相反的操作-即在您关闭该功能时将所有功能都打开。

The following should work for what I think you're attempting: it makes sure that everything is turned off and turns on just the one you're interested in. 以下内容应该可以满足我的尝试:确保所有内容均已关闭,并且仅打开您感兴趣的内容。

$('#momslist li a').click(function() {
    $('#momslist li').removeClass('highlight');
    $(this).parent().addClass('highlight');
});

If you're interested in the "click once to turn on/click again to turn off", the following will do that 如果您对“单击一次以打开/再次单击以关闭”感兴趣,则可以执行以下操作

$('#momslist li a').click(function() {
    var parent = $(this).parent();
    if (parent.hasClass('highlight')) {
        parent.removeClass('highlight');
    } else {
        $('#momslist li').removeClass('highlight');
        parent.addClass('highlight');
    }
});

The approach in the question seems like a lot of dom traversal, but perhaps the most direct solution would be to check to see if the parent has the highlight class on click, if so - don't do anything. 问题中的方法似乎遍历了dom,但是最直接的解决方案可能是检查父项是否具有单击时的highlight类,如果是的话-不要执行任何操作。

$('#momslist li a').click(function() {

    var parent = $(this).parent();

    if (parent.hasClass('highlight')) {return false;}//bail if highlighted

    siblings = parent.siblings(),
    isOn = parent.toggleClass('highlight').hasClass('highlight');
    siblings.toggleClass('highlight', !isOn);

});

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

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