【问题标题】:Styling NavBar with active link使用活动链接样式化 NavBar
【发布时间】:2016-06-02 04:30:48
【问题描述】:

我怎样才能有一个Navbar 突出显示哪些活动项目?

我尝试了一些示例,但无法更改 Active 样式以获得所需的查找。 这是我的_Layout.cshtml 文件:

@{
    ViewBag.AppName = "My Application!";
}


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title @ViewBag.AppName</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid rtl" >
            <div class="navbar-header" style="float:right">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink((string)ViewBag.AppName, "Index", "Home", null, new { @class = "navbar-brand" })
            </div>
            @*<ul class="nav nav-tabs">
                <li>@Html.ActionLink("Home", "Index", "Home")</a></li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            </ul>*@
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li class="active">@Html.ActionLink("Summary", "Index", "Home")</li>
                    <li>@Html.ActionLink("Request1", "Index", "PaymentRequest")</li>
                    <li>@Html.ActionLink("Request2", "Index", "CaptialPaymentRequest")</li>
                    <li>@Html.ActionLink("Request3", "Contact", "Home")</li>
                    <li>@Html.ActionLink("Request4", "Index", "Contract")</li>
                    <li>@Html.ActionLink("Request5", "Contact", "Home")</li>
                </ul>
                @Html.Partial("_LoginPartial")
            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

如果你注意到我有下面的测试行:

        <li class="active">@Html.ActionLink("Summary", "Index", "Home")</li>

但是,我希望它突出显示摘要链接:

1- MVC 自动激活当前页面的链接

2- 活动链接更改的样式(我在我的 Site.css 中使用了下面的 css,但它不起作用)

.navbar #nav > .active > a {
    color: red;
}

这是我的导航栏的样子:

【问题讨论】:

  • 我认为.navbar #nav &gt; .active &gt; a { color: red; } 应该是.navbar #navbar &gt; .active &gt; a { color: red; }
  • @GuruprasadRao 它没有帮助
  • 你没有任何带有id="nav" 的元素(所以#nav 什么都不选)。只需使用.active &gt; a { color: red; }。您是否要根据单击的链接设置class="active" - 即,如果您通过单击“Request2”导航到../CaptialPaymentRequest/Index,那么您希望突出显示“Request2”?

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


【解决方案1】:

试试这个。

在你的 ul 中添加一个 id="nav"

<ul class="nav navbar-nav" id="nav">

然后添加这个脚本

 $(function() {
    $('#nav li a').click(function() {
       $('#nav li').removeClass();
       $($(this).attr('href')).addClass('active');
    });
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多