简体   繁体   English

jQuery幻灯片菜单-更好的方法?

[英]jquery slide menu - better way?

I'm pretty new to javascript/jquery and I just built a simple slide menu. 我是javascript / jquery的新手,我刚刚构建了一个简单的幻灯片菜单。

It has 3 menus and each menu has a submenu...everything is working fine, I just want to know if there's a better way to accomplish the same task. 它有3个菜单,每个菜单都有一个子菜单...一切正常,我只想知道是否有更好的方法来完成相同的任务。

Here's my js code: 这是我的js代码:

function menuOpen(menu){
    if(menu=='menu1'){
        $("#sub2").slideUp(400);
        $("#sub3").slideUp(400);
        $("#sub1").slideToggle(400);
    }else if(menu=='menu2'){
        $("#sub1").slideUp(400);
        $("#sub3").slideUp(400);
        $("#sub2").slideToggle(400);
    }else if(menu=='menu3'){
        $("#sub1").slideUp(400);
        $("#sub2").slideUp(400);
        $("#sub3").slideToggle(300);
    }
}

without seeing your HTML : 没有看到您的HTML

LIVE DEMO 现场演示

function menuOpen(menu){      
    var num = menu.match( /\d+/ ); // Regex expression to retrieve the Number                 
    $('[id^=sub]').slideUp();      // slide UP all ID starting with sub
    $('#sub'+num).slideToggle();   // get the desired ID :)  
}

Use of jQuery means that we want to easily manipulate DOM elements , which means that without seeing a HTML sample of your DOM nodes and structure you're about to target it's hard to make the above even simpler . jQuery的使用意味着我们想要轻松地操作DOM元素,这意味着在没有看到要作为目标的DOM节点和结构的HTML示例的情况下,很难使上述内容变得更加简单

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

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