单击链接后,导航栏中的子菜单将折叠

时间:2015-01-07 18:54:44

标签: javascript html css asp.net-mvc twitter-bootstrap

我的Web应用程序左侧有一个导航栏,菜单的每个项目都有一个可扩展的子菜单,用于浏览页面。不幸的是,每次我点击子菜单的其中一个链接时,都会加载新页面,但子菜单会再次折叠。我希望点击其中一个链接后子菜单仍然展开。

这是我的代码:

<div class="container-fluid">
    <div class="row">
        <div class="col-md-3">
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="nav-header panel panel-default">
                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                        <div class="panel-heading" role="tab" id="headingOne">
                            <h4 class="panel-title">
                                <span class="glyphicon glyphicon-th"></span>Cluster
                            </h4>
                        </div>
                    </a>
                    <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">

                        <div class="panel-body">
                            <div class="nav-item"><span class="glyphicon glyphicon-eye-open"></span>@Html.ActionLink("overview", "ClusterAll", "Cluster", new { area = "" }, new { @class = "nav-item", style = "text-decoration:none;" })</div>
                            <div class="nav-item"><span class="glyphicon glyphicon-plus"></span>@Html.ActionLink("create", "ClusterCreation", "Cluster", new { area = "" }, new { @class = "nav-item", style = "text-decoration:none;" })</div>
                            <div class="nav-item"><span class="glyphicon glyphicon-pencil"></span>@Html.ActionLink("edit", "ClusterEdit", "Cluster", new { area = "" }, new { @class = "nav-item", style = "text-decoration:none;" })</div>
                        </div>
                    </div>
                </div>
                <div class="nav-header panel panel-default">
                    <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">

                        <div class="panel-heading" role="tab" id="headingTwo">
                            <h4 class="panel-title">
                                <span class="glyphicon glyphicon-hdd"></span>Applications
                            </h4>
                        </div>
                    </a>
                    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
                        <div class="panel-body">
                            <div class="nav-item"><span class="glyphicon glyphicon-ok"></span>@Html.ActionLink("verify", "ApplicationPortfolioEdit", "Applications", new { area = "" }, new { @class = "nav-item", style = "text-decoration:none;" })</div>
                            <div class="nav-item"><span class="glyphicon glyphicon-list-alt"></span>@Html.ActionLink("manage", "ApplicationMassEdit", "Applications", new { area = "" }, new { @class = "nav-item", style = "text-decoration:none;" })</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

实际菜单中有更多项目,这只是一个示例,有两个项目可供您展示。 有什么想法,我点击链接后要保持子菜单的扩展吗? 我对网页设计很陌生,但我希望你能帮助我:)。

1 个答案:

答案 0 :(得分:0)

如果我得到了您正在尝试做的事情,您可以将打开的子菜单作为#param传递到您的网址中,这样当他们点击指向新网页的链接时,您可以让Javascript / jQuery读取#并打开适当的子菜单。

像这样......

<a href="page.html#menuItemClicked">Applications</a>

然后使用Javascript&amp; jQuery我们可以用..读取它。

&#13;
&#13;
$(document).ready(function() {
  
  hash = window.location.hash;
  
  $(hash).collapse('show');
  
});
&#13;
&#13;
&#13;

我们可以使用$(hash).collapse('show');来显示内容,因为您正在使用Bootstrap。

但这种印象让我明白你在说什么。 :)