javascript - 单击子链接时如何停止垂直菜单的 Accordion 。?

标签 javascript jquery html css menu

我正在尝试构建一个具有 Accordion 效果的垂直导航菜单。我几乎创建了菜单。但问题是我所有的链接都指向同一个链接。当我点击任何子导航链接时,页面将重新加载,但父菜单正在折叠。即使在页面重新加载后,我也想显示突出显示的事件子链接。请建议我解决这个问题。

这是 Demo

html:

 <!doctype html>
    <html lang="en-US">
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
      <title>Vertical jQuery Accordion Nav Menu</title>
      <meta name="author" content="Jake Rocheleau">
      <link rel="shortcut icon" href="http://vandelaydesign.com/favicon.ico">
      <link rel="icon" href="http://vandelaydesign.com/favicon.ico">
      <link rel="stylesheet" type="text/css" href="styles.css">
      <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Merienda:400,700">
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
      <script type="text/javascript" language="javascript" charset="utf-8" src="nav.js"></script>
    <!--[if lt IE 9]>
      <script type="text/javascript" src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    </head>

    <body>
    <div id="w">
         <h1>CSS3/jQuery Accordion Nav Menu</h1>

        <nav>
            <ul id="nav">
                <li><a href="#">Animation</a>

                    <ul>
                        <li><a href="#">Cartoons</a>
                        </li>
                        <li><a href="#">Comic Strips</a>
                        </li>
                        <li><a href="#">Video Clips</a>
                        </li>
                        <li><a href="#">Web GIFs</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Graphic Design</a>

                    <ul>
                        <li><a href="#">Adobe Photoshop</a>
                        </li>
                        <li><a href="#">Branding &amp; Logos</a>
                        </li>
                        <li><a href="#">Digital Marketing</a>
                        </li>
                        <li><a href="#">Illustrations</a>
                        </li>
                        <li><a href="#">Infographics</a>
                        </li>
                        <li><a href="#">Product Design</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Digital Photography</a>

                    <ul>
                        <li><a href="http:/www.google.com/search?q=cityscape+photography">Cityscapes</a>
                        </li>
                        <li><a href="#">Oceans</a>
                        </li>
                        <li><a href="#">Wide-Angle Lens</a>
                        </li>
                        <li><a href="#">Wildlife</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Print &amp; Identity</a>

                    <ul>
                        <li><a href="#">Branding</a>
                        </li>
                        <li><a href="#">Business Cards</a>
                        </li>
                        <li><a href="#">Letterpress</a>
                        </li>
                        <li><a href="#">Poster Art</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Programming</a>

                    <ul>
                        <li><a href="#">CSS3</a>
                        </li>
                        <li><a href="#">HTML5</a>
                        </li>
                        <li><a href="#">JavaScript &amp; jQuery</a>
                        </li>
                        <li><a href="#">MySQL Databases</a>
                        </li>
                        <li><a href="#">Wordpress CMS</a>
                        </li>
                    </ul>
                </li>
                <li><a href="#">Web Design</a>

                    <ul>
                        <li><a href="#">Icons</a>
                        </li>
#">Tutorials</a>
                        </li>
                        <li><a href="#">User Interfaces</a>
                        </li>
                        <li><a href="#">Website Layouts</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </nav>
    </div>
</body>
    </html>

CSS:

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
  outline: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
html { height: 101%; }
body { font-size: 62.5%; line-height: 1; padding-bottom: 65px; font-family: Arial, Tahoma, sans-serif; background: #c5bde5 url('images/bg.png'); }

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }

blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
strong { font-weight: bold; } 

table { border-collapse: collapse; border-spacing: 0; }
img { border: 0; max-width: 100%; }

h1 { font-family: 'Merienda', 'Trebuchet MS', Verdana, sans-serif; font-size: 2.95em; line-height: 1.7em; margin-bottom: 20px; font-weight: bold; letter-spacing: -0.03em; color: #675d90; text-shadow: 2px 2px 0px rgba(255,255,255,0.65); text-align: center; }


#w { display: block; width: 740px; margin: 0 auto; padding-top: 45px; }


/* nav menu styles */
#nav { 
  display: block; 
  width: 280px; 
  margin: 0 auto; 
  -webkit-box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
  -moz-box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
  box-shadow: 3px 2px 3px rgba(0,0,0,0.7);
}

#nav li { }

#nav > li > a { 
  display: block; 
  padding: 16px 18px;
  font-size: 1.3em;
  font-weight: bold;
  color: #d4d4d4;
  text-decoration: none;
  border-bottom: 1px solid #212121;
  background-color: #343434;
  background: -webkit-gradient(linear, left top, left bottom, from(#343434), to(#292929));
  background: -webkit-linear-gradient(top, #343434, #292929);
  background: -moz-linear-gradient(top, #343434, #292929);
  background: -ms-linear-gradient(top, #343434, #292929);
  background: -o-linear-gradient(top, #343434, #292929);
  background: linear-gradient(top, #343434, #292929);
}
#nav > li > a:hover, #nav > li > a.open { 
  color: #e9e9e9;
  border-bottom-color: #384f76;
  background-color: #6985b5;
  background: -webkit-gradient(linear, left top, left bottom, from(#6985b5), to(#456397));
  background: -webkit-linear-gradient(top, #6985b5, #456397);
  background: -moz-linear-gradient(top, #6985b5, #456397);
  background: -ms-linear-gradient(top, #6985b5, #456397);
  background: -o-linear-gradient(top, #6985b5, #456397);
  background: linear-gradient(top, #6985b5, #456397);
}

#nav li ul { display: none; background: #4a5b78; }

#nav li ul li a { 
  display: block; 
  background: none;
  padding: 10px 0px;
  padding-left: 30px;
  font-size: 1.1em;
  text-decoration: none;
  font-weight: bold;
  color: #e3e7f1;
  text-shadow: 1px 1px 0px #000;
}
#nav li ul li a:hover {
  background: #394963;
}

Java 脚本:

$(document).ready(function(){
  $("#nav > li > a").on("click", function(e){
    if($(this).parent().has("ul")) {
      e.preventDefault();
    }

    if(!$(this).hasClass("open")) {
      // hide any open menus and remove all other classes
      $("#nav li ul").slideUp(350);
      $("#nav li a").removeClass("open");

      // open our new menu and add the open class
      $(this).next("ul").slideDown(350);
      $(this).addClass("open");
    }

    else if($(this).hasClass("open")) {
      $(this).removeClass("open");
      $(this).next("ul").slideUp(350);
    }
  });
});

最佳答案

首先,这些链接会将用户引导至 Google 搜索页面。如果这些只是占位符链接,并且您打算在您自己的站点内引导用户,那么您有很多选择。您是否考虑过使用允许在不加载页面的情况下加载内容的 AJAX 技术? Angular JS是一个很棒的框架,可用于快速创建允许与服务器进行异步交互的单页应用程序。

如果您需要重新加载页面,您同样有多种选择。一种选择是将请求中的参数传递给服务器,您可以使用这些参数以编程方式设置菜单的状态。

关于javascript - 单击子链接时如何停止垂直菜单的 Accordion 。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28497548/

相关文章:

jquery - 根据日期范围过滤更改 jquery 数据表行的 css

javascript - 在 JavaScript 中根据视口(viewport)位置隐藏元素

javascript - 两个容器之间的自定义形状线,像进度条一样改变颜色

html - 映射图像的不透明部分

javascript - 在图像悬停时显示文本

jquery - 使用 jquery 从数组更改图像 src

javascript - 在php中提交或页面刷新后如何保持复选框的选中状态?

Javascript:有这样的语法吗

javascript - 浏览器支持的所有 HTML 标签

javascript - 在 Emacs js2 模式下查找语法警告的热键?