简体   繁体   English

手风琴菜单addclass不起作用

[英]accordion menu addclass not working

I have an accordion menu with background image and color of the heading (menu tab) changing on hover and while clicked. 我有一个手风琴菜单,其背景图像和标题(菜单选项卡)的颜色在悬停时和单击时都在变化。 I would like this also to be the case when that menu tab is open (ie, the image/color indicate which menu is open). 我也希望菜单选项卡处于打开状态(例如,图像/颜色指示哪个菜单处于打开状态)。 Right now, as soon as the cursor is not over the menu heading, the background image is removed and the color reverts to the inactive state. 现在,只要光标不在菜单标题上,背景图像就会被删除,颜色恢复为非活动状态。 All of my research on this site and elsewhere points to using the removeClass and addClass methods, but I can't get this to work. 我在此站点以及其他地方所做的所有研究都指向使用removeClass和addClass方法,但是我无法使其正常工作。

JQuery: jQuery的:

$(document).ready(function(){
    var panel = $("#accordian h3");
    panel.click(function(){
        //slide up all the link lists
        $("#accordian ul ul").slideUp();
        //slide down the link list below the h3 clicked, if it's closed
        if(!$(this).next().is(":visible"))
            $(this).next().slideDown();
        panel.removeClass('active')
        panel.addClass('active')
    });
})

Relevant CSS (minus the UL specs): 相关CSS(减去UL规范):

/*main menu heading style*/
#accordian h3 {
    font-size: 12px;
    line-height: 34px;
    padding: 0px 0px 0px 5px;
    cursor: pointer;
    /*fallback for browsers not supporting gradients*/
    background: #984e06;
    background: linear-gradient(#984e06, #FF9600);
}
/*heading hover effect, active (as clicked) effect*/
#accordian h3:hover,
#accordian h3:active {
    text-shadow: 0 0 2px rgba(0, 0, 0, 1.0);
    background-repeat: no-repeat;
    background-image: url(../Images/Logo.jpg);
    background-size: 25px 25px;
    background-position: 5px 4px; /*left center;*/
    padding: 0px 35px;
    background-color: #ffdf05; /*linear-gradient(#ffdf05, #FF9600);*/
}

I've spent hours trying to find the solution. 我已经花了数小时试图找到解决方案。 Are the removeClass and addClass methods correct? removeClass和addClass方法正确吗? And in the correct location within the code? 并在代码内的正确位置? I've also tried replacing those two lines with one line using the toggleClass method, but that doesn't work either. 我也尝试过使用toggleClass方法将这两行替换为一行,但这都不起作用。 Thanks for your help. 谢谢你的帮助。

I've removed some of your code and I've changed it a little bit I believe this is what you've meant: 我删除了一些代码,并做了一些更改,我相信这就是您的意思:

$(document).ready(function(){ 

    $('#accordian ul').hide();

    $("#accordian h3").click(function(){

        $("#accordian ul").slideUp();
          //type here the css that you want if it's NOT clicked            
          $('#accordian h3').css({
              'background-color' : '#984e06', 
              'background-position': '5px 4px'
          });

        if(!$(this).next().is(":visible"))
        {
            $(this).next().slideDown();
            //type here the css that you want if it's clicked            
            $(this).css({
                'background-color' : '#ffdf05', 
                'background-position': '5px 4px'
            });

        }

    });

});

Example: 例:
http://jsfiddle.net/xvmq8g9k/ http://jsfiddle.net/xvmq8g9k/

Edited on Line 9: instead of using $(this), I've changed it to $('#accordian h3') so all the h3 will be revert instead of the one that you click like you wanted 在第9行上进行了修改:我将其更改为$('#accordian h3'),而不是使用$(this),因此所有h3都将还原,而不是您想要的那样单击的那个

Let me know if you have more questions 如果您还有其他问题,请告诉我

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

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