英文:
Add Class to Header Nav for all Actions within a Controller
问题
我有一个ASP.NET MVC网站,其中有一个标准的导航栏,显示在标题中指向特定控制器和操作的文本。当我导航到localhost/ 时,将会向 "Home" 添加 "text-white bg-primary" 类。当我导航到localhost/Entry 时,这些类将被添加到 "Entry"。
然而,当我导航到除这两个目录之外的任何其他目录(例如localhost/Home/Index,localhost/Home/GetJobs,localhost/Entry/Review)时,这些类不会被添加。我期望的功能是在 localhost/Home/* 下,"Home" 导航项也添加这些类,以及在 localhost/Entry/* 下,"Entry" 导航项也添加这些类。
<nav class="navbar navbar-expand-md navbar-light bg-light header-box-shadow">
<div class="collapse navbar-collapse" id="appNavigation">
<div class="navbar-nav">
<a class="nav-link" asp-action="Index" asp-controller="Home">Home</a>
<a class="nav-link" asp-action="Index" asp-controller="Entry">Manual Entry</a>
</div>
</div>
</nav>
<script type="text/javascript">
$("nav a.nav-link[href='" + location.pathname + "']").addClass("text-white bg-primary");
</script>
英文:
I have an ASP.NET MVC website which has a standard navbar that displays text in the header that points to a certain controller and action. When I navigate to localhost/ the text-white bg-primary classes are added to "Home". When I navigate to localhost/Entry the classes are added to "Entry".
However, when I navigate to anything other than those two directories (i.e. localhost/Home/Index, localhost/Home/GetJobs, localhost/Entry/Review) the classes are not added. My desired functionality is for the "Home" nav item to have the classes added to it for localhost/Home/* as well as for "Entry" .. localhost/Entry/*
<nav class="navbar navbar-expand-md navbar-light bg-light header-box-shadow">
<div class="collapse navbar-collapse" id="appNavigation">
<div class="navbar-nav">
<a class="nav-link" asp-action="Index" asp-controller="Home">Home</a>
<a class="nav-link" asp-action="Index" asp-controller="Entry">Manual Entry</a>
</div>
</div>
</nav>
<script type="text/javascript">
$("nav a.nav-link[href='" + location.pathname + "']").addClass("text-white bg-primary");
</script>
答案1
得分: 0
以下是您要翻译的内容:
我能够使这个适用于我的用例。 如果您的导航链接到一个控制器下的多个不同操作,可能需要进行一些修改。
<!-- language: lang-js -->
<script type="text/javascript">
let elements = document.getElementsByClassName("nav-link");
// shows current nav item for any page/action in the controller
// e.g. /Home/Index, /Home/Search, etc. will change CSS of "Home" to show as the current nav item.
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
let href = element.attributes.getNamedItem("href").value.toString();
// default page, "/" .. set href to default controller for proper matching
if (href === "/") {
// TODO: "get" default controller set by Endpoints Controller Route
href = "/Home";
}
let path = location.pathname.split('/');
path.pop();
path = path.join('').toLowerCase();
if (href.toLowerCase().includes(path)) {
$(element).addClass("text-white bg-primary");
// path is an empty string when at the default page "/" to avoid mismatches, stop the for loop from continuing
if (path.length < 1) {
break;
}
}
}
</script>
<!-- end snippet -->
请注意,代码中的注释没有被翻译,只翻译了代码本身。
英文:
I was able to make this work for my use case. This would need some modification if your nav has links to multiple different actions under one controller.
<!-- language: lang-js -->
<script type="text/javascript">
let elements = document.getElementsByClassName("nav-link");
// shows current nav item for any page/action in the controller
// e.g. /Home/Index, /Home/Search, etc. will change CSS of "Home" to show as the current nav item.
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
let href = element.attributes.getNamedItem("href").value.toString();
// default page, "/" .. set href to default controller for proper matching
if (href === "/") {
// TODO: "get" default controller set by Endpoints Controller Route
href = "/Home";
}
let path = location.pathname.split('/');
path.pop();
path = path.join('').toLowerCase();
if (href.toLowerCase().includes(path)) {
$(element).addClass("text-white bg-primary");
// path is an empty string when at the default page "/" to avoid mismatches, stop the for loop from continuing
if (path.length < 1) {
break;
}
}
}
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论