简体   繁体   English

带有JavaScript链接问题的手风琴菜单

[英]Accordion menu with javascript link issue

I made this list accordion menu in javascript with a tutorial. 我在javascript中通过教程制作了此列表手风琴菜单。

as you see : each main li class "ToggleSubmenu" is transformed to havn't any link anymore but just be a category to hide / show the other sub li. 如您所见:每个主li类“ ToggleSubmenu”都已转换为不再具有任何链接,而只是一个隐藏/显示另一个子li的类别。

my question is : how to have the same effect but have the ToggleSubmenu having a link AND having the show/hide function ? 我的问题是:如何具有相同的效果,但是ToggleSubmenu具有链接并且具有show / hide功能?

as you see the return false block the link, but i can't just remove it. 如您所见,return false将阻止该链接,但我不能仅仅将其删除。

edit : ideally, how to add a class named ".linked" to say "if there is this class .linked, activate a specific link. if not do the same it already is" 编辑:理想情况下,如何添加一个名为“ .linked”的类来表示“如果存在.link类,请激活特定链接。如果不这样做,则已经是相同的”

Thank you very much for your help. 非常感谢您的帮助。

Benj 本杰

$(document).ready( function () {
// all the sub menu are hidden
// except "open_at_load" one :
$(".navigation ul.subMenu:not('.open_at_load')").hide();
// we select all the list items with class "toggleSubMenu"

// we replace their span by a link :
$(".navigation li.toggleSubMenu span").each( function () {
    // we stock what is inside the span :
    var TexteSpan = $(this).text();
    $(this).replaceWith('<a href="" title="Afficher le sous-menu">' + TexteSpan + '<\/a>') ;
} ) ;

// we modify the "click" event on the linksin the list items
// wich have the class "toggleSubMenu" :
$(".navigation li.toggleSubMenu > a").click( function () {
    // Si le sub menu was already open, we hide it:
    if ($(this).next("ul.subMenu:visible").length != 0) {
        $(this).next("ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") } );
    }
    // if the sub menu is hidden, we close the other and we show it :
    else {
        $(".navigation ul.subMenu").slideUp("normal", function () { $(this).parent().removeClass("open") });
        $(this).next("ul.subMenu").slideDown("normal", function () { $(this).parent().addClass("open") } );
    }
    // we stop the link :
    return false;

});

I would suggest using data attributes. 我建议使用数据属性。 Add a data attribute for every accordion button you want to have a link (ie call it data-linkeaccordion). 为您想要链接的每个手风琴按钮添加一个数据属性(即,将其称为data-linkeaccordion)。

Create a specific click handle on every element having this data attr to open a new window , the url being in the data attr value. 在具有此数据属性的每个元素上创建一个特定的单击句柄,以打开一个新窗口,URL位于数据属性值中。

This will not mess up with the existing accordion event. 这不会与现有的手风琴事件混为一谈。

您可以在函数返回中简单地执行此操作

return $(this).hasClass("linked");

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

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