简体   繁体   English

在Sidenav菜单中创建子菜单(ASP.NET MVC)

[英]Make submenu in Sidenav menu(ASP.NET MVC)

I have side menu 我有侧面菜单

Here is code of it 这是它的代码

 <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <a href="@Url.Action("Index", "Home")">Home</a>
    <a href="@Url.Action("Index", "Calendar")">Calendar</a>
    <a href="@Url.Action("Index", "PatientDatabase")">Patient Database</a>
    <a href="@Url.Action("Index", "Findings")">Findings</a>
    <a href="@Url.Action("Index", "Controlling")">Controlling</a>
    <a href="@Url.Action("Index", "Invoices")">Invoices</a>
</div>

And JS that's work with it 和它一起工作的JS

    <script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
}
</script>

One of my menu point will have submenu. 我的菜单点之一将具有子菜单。

So when I click for example Findings I need to see submenu that raleted to Findings . 因此,当我单击例如Findings我需要查看子菜单,该子菜单排到了Findings

How I can realize this? 我怎么能意识到这一点?

Two quick solutions: 两种快速解决方案:

1. JavaScript 1. JavaScript

instead of <a href="@Url.Action("Index", "Findings")">Findings</a> 而不是<a href="@Url.Action("Index", "Findings")">Findings</a>

have the markup like 有像这样的标记

<a class="submenu" href="@Url.Action("Index", "Findings")">
  <p>Findings</p>
  <a class="submenu-item" href="#">Sub menu item</a>
  <a class="submenu-item" href="#">Sub menu item</a>
</a>

with a stylesheet like 像这样的样式表

.submenu > .submenu-item { display: none }
.submenu.open > .submenu-item { display: block }

and your JavaScript like 和你的JavaScript一样

document.querySelectorAll('.submenu').forEach(function(item) {
  item.addEventListener('click', function(event) {
    event.preventDefault()
    item.classList.toggle('open')
  })
})

That's a simple JavaScript toggle for hiding and showing items. 这是用于隐藏和显示项目的简单JavaScript切换。 The reason I kept the anchor tag on the top level item is so that it still works if JS doesn't. 我将锚标记保留在顶层项目的原因是,即使JS不起作用,它也仍然可以工作。

2. Detail tag 2.详细标签

HTML has a native tag that shows and hides item HTML具有显示和隐藏项目的本机标记

<detail>
  <summary>Findings</summary>
  <a href="#">Sub menu item</a>
  <a href="#">Sub menu item</a>
</detail>

However, browser support is not at 100% . 但是, 浏览器支持并非100%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM