0

First of all, forgive me if this has already been answered, I searched but couldn't find an example relevant to what I'm trying to do.

I have a vertical menu with some sub menus. When the top level menu is clicked the sub menu opens up. The problem is that I would like the sub menu you clicked to stay open when you arrive to the new page. As it is now, when a sub-menu item is clicked and you go to the destination page, the menu collapses.

Here's my code:

<script type="text/javascript" charset="utf-8">
$(function(){
    $('#menu li a').click(function(event){
        var elem = $(this).next();
        if(elem.is('ul')){
            event.preventDefault();
            $('#menu ul:visible').not(elem).slideUp();
            elem.slideToggle();
        }
    });
});
</script>

And a Fiddle:

http://jsfiddle.net/4fYS6/

Thanks in advance

4

2 回答 2

0

当您完全加载第二页时,您可以将选定的UL索引传递回服务器并作为响应返回,然后在页面加载时UL再次打开它。

类似于下面的代码......(不是确切的代码。)演示

$(function(){
    $('#menu li a').click(function(event){
        var elem = $(this).next();
        if(elem.is('ul')){
            event.preventDefault();
            $('#menu ul:visible').not(elem).slideUp();
            elem.slideToggle();
        } else {
          alert("Parent Index "+$('#menu ul').index($(this).parent().parent()));
            $(this).attr("href" ,$(this).attr("href")+"?MenuIndex="+ $('#menu ul').index($(this).parent().parent()));

        }

    });
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

    if(getURLParameter("MenuIndex")) {
     $('#menu ul').eq(getURLParameter("MenuIndex")).click();
    }
});
于 2013-05-28T14:00:44.423 回答
0

如果您想避免使用 cookie,请尝试此操作

HTML

  <iframe  src="" name='frame' >
  </iframe>  

<li><a href="http://www.bing.com" target="frame">Submenu 1</a></li>

CSS

div{
  display:inline-block;
}
#frm{
  height:500px;
  width:500px;
 border:0px solid black;

}

演示

根据您的需要调整样式。

于 2013-05-28T13:33:17.453 回答