2013-06-25 25 views
0

导航菜单 - 菜单需要时,页面被点击另一个链接刷新保持开放单击手风琴菜单中的链接时,一旦页面刷新了新的html内容,如何保持手风琴菜单打开?

ul class="menu"> 
        <li class="item1"><a href="#">Hardware</a> 
         <ul> 
          <li class="subitem1"><a href="receipt-printer.html">Receipt Printer</a></li> 
          <li class="subitem2"><a href="barcode-scanner.html">Barcode <span class="item-move2">Scanner</span> </a></li> 
          <li class="subitem3"><a href="card-swipes.html">Card Swipes</a></li> 
          <li class="subitem4"><a href="weighscales.html">Weighscales</a></li> 
          <li class="subitem5"><a href="touchscreens.html">Touchscreens</a></li> 
          <li class="subitem6"><a href="label-printer.html">Label Printer</a></li> 
          <li class="subitem7"><a href="customer-display.html">Customer <span class="item-move2"> Display</span></a></li> 
          <li class="subitem8"><a href="wap.html">WAP</a></li> 
         </ul> 
        </li> 
        <li class="item2"><a href="#">Software</a> 
         <ul> 
          <li class="subitem1"><a href="step1/install-ootpik.html">Installing ootpik</a></li> 
          <li class="subitem2"><a href="step1/pref-security.html">Preferences &amp; <span class="item-move2">Security</span></a></li> 
         </ul> 
        </li> 
       </ul> 

菜单 端脚本来操作折叠式菜单脚本

<!--initiate accordion--> 
<script type="text/javascript"> 
    $(function() { 

     var menu_ul = $('.menu > li > ul'), 
       menu_a = $('.menu > li > a'); 

      menu_ul.hide(); 

     menu_a.click(function(e) { 
      e.preventDefault(); 
      if(!$(this).hasClass('active')) { 
       menu_a.removeClass('active'); 
       menu_ul.filter(':visible').slideUp('normal'); 
       $(this).addClass('active').next().stop(true,true).slideDown('normal'); 
      } else { 
       $(this).removeClass('active'); 
       $(this).next().stop(true,true).slideUp('normal'); 
      } 

     }); 

    }); 
</script> 

网站查看手风琴现在: http://www.ootpik.info/lauren/ootpik5/receipt-printer.html

+1

你确实应该尝试将这一数字减少到仅仅需要什么来解释和演示问题/问题。这是一大堆代码。你还应该在问题的主体部分详细解释问题/问题,而不仅仅是问题的标题。 –

+0

代码更改....我可以保持打开状态,但我没有正确刷新。它将锁定手风琴 – Lauren

+0

我认为问题是如何让我们的JavaScript超越http请求/响应...不,我只是在开玩笑,我也不明白这个问题。如果你有一些后端,你可以通过会话来配置你的javascript。 – rusln

回答

1

我建议你尝试另一个菜单选择器根据你当前的URL(因为当你的页面刷新你不能得到菜单项之前点击)。它可能是这样的东西(对于receipt-printer.html页面):

$('.menu > li > ul:not(:has(li > a[href="receipt-printer.html"]))') 

这是硬编码,但我认为它可以为你工作。 您可以从window.location.pathname获得当前页面的值。例如像这样:

var url = window.location.pathname.split('/')[3] //because on your site you have relative url lauren/ootpik5/receipt-printer.html 

但它也硬编码,我不知道它会适用于您的网站上的任何网址。

算法如此充满例子是:

var splittedUrl = window.location.pathname.split('/'); 
var href = splittedUrl[splittedUrl.length - 1]; 
var menu_ul = $('.menu > li > ul:not(:has(li > a[href="' + href + '"]))'); 
munu_ul.hide(); 
+0

我想我会使用不同的菜单选择器,这只是隐藏我的导航完全离开页面,但是谢谢 – Lauren

+0

我在使用这种手风琴菜单系统时找到了一种方法。无论您想使用什么,您都必须使用javascript或ajax动态获取所有页面内容。这也会遇到另一个无法使用后退或前进按钮的问题。所以从那时起,你需要使用hashchange来使用所有的前进和后退按钮 – Lauren

+0

如果你想使用ajax并保持后退和前进按钮的功能,请查看历史API https://developer.mozilla.org/en -US/docs/Web/Guide/DOM/Manipulating_the_browser_history。它不适用于所有浏览器(http://caniuse.com/#search=history),但存在一些polyfills:https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills –