我一直在尝试突出显示项目中的"活动导航"选项卡.我的任务是更新旧网站而不更改为bootstrap(这是我的经验).我发现了一个拥有我需要的大部分内容的例子.现在,唯一具有"选定类"的选项卡是"主页"选项卡.当我单击另一个选项卡时,"主页"选项卡不再突出显示,但当前选项卡不再突出显示.当我检查元素时,Home选项卡仍然具有"selected class"属性.不知道该怎么办.
_布局
<ul id="navigation">
<li class="@Html.ActivePage("Home", "Index")">@Html.ActionLink("Home", "Index", "Home")</li>
<li class="@Html.ActivePage("About", "About")">@Html.ActionLink("About", "About", "Home")</li>
<li class="@Html.ActivePage("Products", "Products")">@Html.ActionLink("Products", "Products", "Home")</li>
<li class="@Html.ActivePage("Services", "Services")">@Html.ActionLink("Services", "Services", "Home")</li>
<li class="@Html.ActivePage("Contact", "Contact")">@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
HtmlHelper类
public static class HtmlHelperExtensions
{
public static string ActivePage(this HtmlHelper helper, string controller, string action)
{
string classValue = "";
string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();
if (currentController == controller && currentAction == action)
{
classValue = "selected";
}
return classValue;
}
}
Run Code Online (Sandbox Code Playgroud)
CSS
ul#navigation li …Run Code Online (Sandbox Code Playgroud)