Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS菜单保持菜单状态_Javascript_Css - Fatal编程技术网

Javascript JS菜单保持菜单状态

Javascript JS菜单保持菜单状态,javascript,css,Javascript,Css,我有以下HTML代码: <div id="nav"> <li><a href="/admin/index.php" target="_top">Dashboard</a></li> <li><a>Contacts</a> <ul> <li><strong>Companies</strong></li> <li&g

我有以下HTML代码:

<div id="nav">
<li><a href="/admin/index.php" target="_top">Dashboard</a></li>
<li><a>Contacts</a>
    <ul>
    <li><strong>Companies</strong></li>
    <li><a href="/admin/customer/addcustomer.php" target="crm_frame">Add Company</a></li>
    <li><a href="/admin/customer/viewcustomer.php" target="crm_frame">View Company</a></li>
    </ul>
</li>
</div>

当页面更改时,单击处理程序将被绑定,但没有处理菜单初始状态的语句

所以

祝你好运! 跟踪当前活动页面的一个快速但有点脏的解决方案是将目标框架的src属性与链接的href属性进行比较


编辑:下面的提琴可能会对您有所帮助:

我建议在此场景中使用sessionStorage。在这种情况下,它是一个很好的工具,并且得到了广泛的支持,但是看看它是否适合您的需要。您可以使用sessionStorage跟踪当前展开菜单的客户端,以便在页面重新加载时展开正确的菜单。这个答案不是100%正确的,因为你不能直接把它插入到你的代码中,我不得不猜测一些事情,但它应该给你一个大致的方向。请注意,在下面的代码中,我将link hrefs改为指向JSFiddle,因为我在这里制作了一个工作示例,但希望这将使您走上正确的轨道,在自己的页面中实现它

需要改变的主要事情之一是给出主菜单 注意,由于JSFiddle是如何构建的,前面的链接是直接指向JSFiddle结果iframe is的链接。否则,当点击链接时,JSFIDLE就会中断。实际的JSFIDLE在这里:


您可以使用cookies…这一行将附加到您的文档就绪回调函数中。我猜你的页面没有刷新看到你链接上的目标属性?这些目标属性现在没有被使用-所有链接刷新页面,它们不再在框架中打开我只是没有删除它们Yet你知道我如何在我的情况下实现你的解决方案吗?
<script>
$(document).ready(function () {
  $('#nav > li > a').click(function(e){
     if ($(this).attr('class') != 'active'){
       $('#nav li ul').slideUp();
       $(this).next().slideToggle();
       $('#nav li a').removeClass('active');
       $(this).addClass('active');
     }
  });
});
</script>
#nav {
    float: left;
    margin-left:5px;
    margin-top:-20px;
    top:0;
    left:0;
    width: 100%; 
    list-style:none;
}
#nav li a {
    display: block; 
    padding: 8px 10px;
    margin-bottom:0;
    background: #666666; 
    border-top: 1px solid #EEEEEE; 
    border-bottom: 1px solid #EEEEEE; 
    text-decoration: none; 
    color: #EEEEEE;
    width:155px;
}
#nav li a:hover, #nav li a.active {
    background: #F36F25; 
    color: #FFFFFF;
    cursor:pointer;
}
#nav li ul {
    display: none;
    list-style:none;
}
#nav li ul li {
    margin-top:0;
    margin-right:0;
    margin-bottom:0;
    margin-left:-40px;
}
#nav li ul li a {
    background: #EEEEEE;
    color:#666666;
    border:1px solid #EEEEEE;
}
#nav li ul li a:hover {
    background: #EEEEEE;
    color:#f36f25;
    border:1px solid #f36f25;
}
$(document).ready(function() {
    //original click handler
    //$('#nav a').click     

    //but also this piece of code, that will display all the lists having an .active link inside
    $('#nav ul').has('a.active').show();
});
<br/>
<div id="nav">
<li>
    <a id="menuDashboard">Dashboard</a>
    <ul>
        <li><strong>Sub Category</strong></li>
        <li><a href="http://jsfiddle.net/VBLS8/2/show/">Sample 1</a></li>
        <li><a href="http://jsfiddle.net/VBLS8/2/show/">Sample 2</a></li>
    </ul>

</li>
<li>
    <a id="menuContacts">Contacts</a>
    <ul>
        <li><strong>Companies</strong></li>
        <li><a href="http://jsfiddle.net/VBLS8/2/show/">Add Company</a></li>
    <li><a href="http://jsfiddle.net/VBLS8/2/show/">View Company</a></li>
</ul>
$(document).ready(function(){
    //Loop through nav items, compare to expanded item ID from sessionStorage so we can expand whichever item was previously expanded
    if(sessionStorage.getItem("activeMenuItemID") != undefined){
        $("#nav > li > a").each(function(){
            if ($(this).attr("id") == sessionStorage.getItem("activeMenuItemID")){
                expandMenuItem(this);
            }
        });
    }

    $('#nav > li > a').click(function(elem){
        expandMenuItem(this);
    });

});

function expandMenuItem(elem){
    if ($(elem).attr('class') != 'active'){
        $('#nav li ul').slideUp();
        $('#nav > li > a').removeClass("active");
        $(elem).addClass("active");
        $(elem).next().slideToggle();
        sessionStorage.setItem("activeMenuItemID", $(elem).attr("id"));
    }
}