保持下拉列表中基于URL的sidenav打开

时间:2018-08-14 13:34:30

标签: javascript jquery html css

我正在使用本教程制作菜单:https://www.w3schools.com/howto/howto_js_dropdown_sidenav.asp

我添加了以下代码以突出显示当前选中的链接:

$("#sidenav a").each(function() {   
    if (this.href == window.location.href) {
        $(this).addClass("active");
    }
});

如果突出显示下拉菜单中的链接,如何使下拉菜单保持打开状态? 只需添加-我的sidenav包含多个下拉菜单。

编辑 我的HTML:

var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    //	    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}

$("#sidenav a").each(function() {
  if (this.href == window.location.href) {
    $(this).addClass("active");
  }
});
.sidenav {
  height: 100%;
  width: 16%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #212529;
  overflow-x: hidden;
}

.sidenav a,
.dropdown-btn {
  padding: 11px 8px 11px 16px;
  text-decoration: none;
  color: #ffffff;
  display: block;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  outline: none;
}

.sidenav a:hover,
.dropdown-btn:hover {
  background-color: #808080;
  color: #f1f1f1;
}

a.active {
  background-color: #002f7c;
}

.main {
  margin-left: 200px;
  font-size: 20px;
  padding: 0px 10px;
}

.current-menu-item {
  background: #33b5e5;
}

.dropdown-container {
  display: none;
  background-color: #262626;
  padding-left: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidenav" id="sidenav">
  <a href="/users"> Users </a>

  <button class="dropdown-btn">Computers</button>
  <div class="dropdown-container">
    <a href="/computers">Assigned</a>
    <a href="/computers/unassigned">Unassigned</a>
  </div>

  <button class="dropdown-btn">Monitors</button>
  <div class="dropdown-container">
    <a href="/monitors">Assigned</a>
    <a href="/monitors/unassigned">Unassigned</a>
  </div>

  <a href="/licenses"> Licenses </a>

  <a href="/reports"> Reports </a>
  <a href="/logs"> Logs </a>
</div>

2 个答案:

答案 0 :(得分:0)

使用jquery函数closest,您可以使用特定的选择器选择$(this).closest(".dropdown-btn"),选择最接近的父元素,选择类“ dropdown-btn”的最接近的父元素。 因此,选择该选项后,您可以在其上模拟点击动作或使其直接可见。

var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
  dropdown[i].addEventListener("click", function() {
    //	    this.classList.toggle("active");
    var dropdownContent = this.nextElementSibling;
    if (dropdownContent.style.display === "block") {
      dropdownContent.style.display = "none";
    } else {
      dropdownContent.style.display = "block";
    }
  });
}

$("#sidenav a").each(function() {
  if (this.href == window.location.href) {
    $(this).addClass("active");
    $("dropdown-btn").css({display : "none"});
    $(this).closest(".dropdown-btn").css({display : "block"});
  }
});
.sidenav {
  height: 100%;
  width: 16%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #212529;
  overflow-x: hidden;
}

.sidenav a,
.dropdown-btn {
  padding: 11px 8px 11px 16px;
  text-decoration: none;
  color: #ffffff;
  display: block;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  outline: none;
}

.sidenav a:hover,
.dropdown-btn:hover {
  background-color: #808080;
  color: #f1f1f1;
}

a.active {
  background-color: #002f7c;
}

.main {
  margin-left: 200px;
  font-size: 20px;
  padding: 0px 10px;
}

.current-menu-item {
  background: #33b5e5;
}

.dropdown-container {
  display: none;
  background-color: #262626;
  padding-left: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidenav" id="sidenav">
  <a href="/users"> Users </a>

  <button class="dropdown-btn">Computers</button>
  <div class="dropdown-container">
    <a href="/computers">Assigned</a>
    <a href="/computers/unassigned">Unassigned</a>
  </div>

  <button class="dropdown-btn">Monitors</button>
  <div class="dropdown-container">
    <a href="/monitors">Assigned</a>
    <a href="/monitors/unassigned">Unassigned</a>
  </div>

  <a href="/licenses"> Licenses </a>

  <a href="/reports"> Reports </a>
  <a href="/logs"> Logs </a>
</div>

答案 1 :(得分:0)

如果使用jQuery,则可以选择链接容器的上一个元素,然后在其上触发click事件,以调用用于切换下拉菜单的函数。因此,您可以使用以下代码段,只需将$(this).attr("href") === "#1"替换为this.href == window.location.href

$(".dropdown-btn").click(function() {
  $(this).toggleClass("active").next().toggle();
});

$(".sidenav a").each(function() {
  if ($(this).attr("href") === "#1") {
      $(this)
        .addClass("active")
        .closest(".dropdown-container")
        .prev(".dropdown-btn")
        .trigger("click");
    }
});
/* Fixed sidenav, full height */
.sidenav {
  height: 100%;
  width: 200px;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  padding-top: 20px;
}

/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 20px;
  color: #818181;
  display: block;
  border: none;
  background: none;
  width:100%;
  text-align: left;
  cursor: pointer;
  outline: none;
}

/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover {
  color: #f1f1f1;
}

/* Main content */
.main {
  margin-left: 200px; /* Same as the width of the sidenav */
  font-size: 20px; /* Increased text to enable scrolling */
  padding: 0px 10px;
}

/* Add an active class to the active dropdown button */
.active {
  background-color: green;
  color: white;
}

/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
  display: none;
  background-color: #262626;
  padding-left: 8px;
}

/* Optional: Style the caret down icon */
.fa-caret-down {
  float: right;
  padding-right: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="sidenav">
  <a href="#about">About</a>
  <a href="#services">Services</a>
  <a href="#clients">Clients</a>
  <a href="#contact">Contact</a>
  <button class="dropdown-btn">Dropdown 
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-container">
    <a href="#1">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  <a href="#contact">Search</a>
</div>