簡體   English   中英

jQuery手風琴菜單活動打開

[英]Jquery accordion menu active open

我有一個工作的jQuery手風琴菜單。 有一件事情仍然不會成功。 當我打開菜單並單擊鏈接時,菜單再次關閉。 我希望菜單保持打開狀態,並且僅在打開另一個菜單時關閉。

對不起我的英語不好。 jQuery代碼

$(document).ready(function(){
          $('.item').click(function(){
                if($(this).css('max-height') == '37px') {
                    $(this).css('max-height','240px')
                }else {
                      $(this).css('max-height','37px')
                }
          });
    });

我希望有人能幫助我。

這是用於html和css的jsfiddle: http : //jsfiddle.net/aAn8n/

嘗試這個 :

$(document).ready(function(){
              $('.item').click(function(){
                    if($(this).css('max-height') == '24px') {
                        $(this).css('max-height','240px')
                    }else {
                          $(this).css('max-height','24px')
                    }
              });
    $('[href^="#"]').click(function(e){
     e.stopPropagation();// this will stop propagation of click event to its parent
     // and hence it will not fire click event for div with class=item
    });
        });

的jsfiddle

可以使用類並在單擊時切換它來代替css外觀,以避免子元素菜單關閉,如果不是頂級菜單,則可以停止該元素的傳播。

參考: toggleClassremoveClassnot

CSS:

#accordion .item.opened {
    max-height: 240px;
}

碼:

$(document).ready(function () {

    $('.item').click(function () {
        $('.item').not($(this)).removeClass("opened");
        $(this).toggleClass("opened");
    });

    $('.item a:not(.kop)').click(function (e) {
        e.stopPropagation();
    })

});

演示: http : //jsfiddle.net/IrvinDominin/8Jt4G/

試試這個代替

$('.kop').click(function(){
        var $parent = $(this).parent(".item");

        if($parent.css('max-height') == '24px') {
            $(".item").css('max-height','24px');
            $parent.css('max-height','240px');
        }else {
            $parent.css('max-height','24px');
        }
});

工作演示

檢查這個演示小提琴

$('a[href^="#"]').click(function(e){
    e.stopPropagation();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM