我正在尝试借助w3schools.com示例在边栏中创建一个下拉列表。由于某种原因,我的代码在codepen.io和stackoverflow上都可以正常工作,但是一旦在Chrome浏览器上运行它,下拉菜单就不会弹出。有什么我能做的吗?我已经清除了浏览历史记录和缓存,但没有任何解决方法。
有人可以帮我吗?
(警告,编码新手)
/*Toggle sidebar*/
function openNav() {
document.getElementById("mySidebar").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("mySidebar").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
/*Dropdown in sidebar*/
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";
}
});
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/*Sidebar with collapseble dropdown from w3school.com */
.sidebar{
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidebar a, .dropdown-btn{
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
border: none;
background: none;
text-align: left;
cursor: pointer;
outline: none;
width:100%;
transition: 0.3s;
}
.sidebar a:hover, .dropdown-btn:hover{
color: #f1f1f1;
}
/* 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-square-down {
float: right;
padding-right: 8px;
}
/* Add an active class to the active dropdown button */
.active {
background-color: green;
color: white;
}
.sidebar .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
.openbtn {
font-size: 20px;
cursor: pointer;
background-color: #111;
color: white;
padding: 10px 15px;
border: none;
}
.openbtn:hover {
background-color: #444;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidebar {padding-top: 15px;}
.sidebar a {font-size: 18px;}
}
<div id="mySidebar" class="sidebar">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<button class="dropdown-btn active">Dropdown
<i class="far fa-caret-square-down"></i>
</button>
<div class="dropdown-container">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<button class="openbtn" onclick="openNav()">☰</button>
<h2>Sidebar Dropdown</h2>
<p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>
<p>This sidebar is of full height (100%) and always shown.</p>
<p>Some random text..</p>
</div>
答案 0 :(得分:0)
如果您的代码在Codepen上工作,则可能是因为您的javascript在页面加载之前执行了(他自己的codepen句柄)
尝试在其中运行所有JS:
document.addEventListener('DOMContentLoaded', function() {
// All your JS here
}, false);