我试图让两个单独的导航菜单独立运行,具体取决于用户点击的按钮,并显示在同一个地方(左)。我有2个单独的功能,但只能让两个按钮打开相同的导航菜单(链接1,2,3不是链接4,5,6)。我可以打开第二个菜单,但前提是我将ClassName更改为topnav而不是sidenav。我也试过getElementByid,但它也不起作用。谢谢你的帮助。
<div class="w3-half w3-container">
<div id="FloatingBox" class="w3-card-16 w3-round w3-blue-grey w3-animate-opacity w3-animate-bottom" style="font-size:xx-large">
<p><a title="Personal Information" onclick="w3_openpers()" class="w3-btn-floating w3-card-8 w3-animate-bottom w3-ripple w3-theme w3-red"><i class="fa fa-plus"></i></a>
Personal
</p>
</div>
</div>
<div class="w3-half w3-container">
<div id="FloatingBox" class="w3-card-16 w3-round w3-blue-grey w3-animate-opacity w3-animate-bottom" style="font-size:xx-large; ">
<p><a title="Professional Information" onclick="w3_openprof()" class="w3-btn-floating w3-card-8 w3-animate-bottom w3-ripple w3-theme w3-red"><i class="fa fa-plus"></i></a>
Professional
</p>
</div>
</div>
<!--- Side Navigation Personal--->
<nav id="persnav" class="w3-sidenav w3-white w3-card-2 w3-animate-left" style="display:none;z-index:5">
<a href="javascript:void(0)">Link 1</a>
<a href="javascript:void(0)">Link 2</a>
<a href="javascript:void(0)">Link 3</a>
<br>
<a href="javascript:void(0)" onclick="w3_closepers()" class="w3-closenav w3-text-theme w3-text-red" >Close ×</a>
</nav>
<!--- Script for Side Navigation Personal--->
<script>
function w3_openpers() {
document.getElementsByClassName("w3-sidenav")[0].style.width = "20%";
document.getElementsByClassName("w3-sidenav")[0].style.textAlign = "center";
document.getElementsByClassName("w3-sidenav")[0].style.fontSize = "30px";
document.getElementsByClassName("w3-sidenav")[0].style.paddingTop = "20%";
document.getElementsByClassName("w3-sidenav")[0].style.display = "block";
document.getElementsByClassName("w3-sidenav")[0].style.opacity = "1";
}
function w3_closepers() {
document.getElementsByClassName("w3-sidenav")[0].style.display = "none";
}
</script>
<!--- Side Navigation Professional--->
<nav id="profnav" class="w3-sidenav w3-white w3-card-2 w3-animate-right" style="display:none;z-index:5">
<a href="javascript:void(0)">Link 4</a>
<a href="javascript:void(0)">Link 5</a>
<a href="javascript:void(0)">Link 6</a>
<br>
<a href="javascript:void(0)" onclick="w3_closeprof()" class="w3-closenav w3-text-theme w3-text-red" >Close ×</a>
</nav>
<!--- Script for Side Navigation Professional--->
<script>
function w3_openprof() {
document.getElementsByClassName("w3-sidenav")[0].style.width = "20%";
document.getElementsByClassName("w3-sidenav")[0].style.textAlign = "center";
document.getElementsByClassName("w3-sidenav")[0].style.fontSize = "30px";
document.getElementsByClassName("w3-sidenav")[0].style.paddingTop = "20%";
document.getElementsByClassName("w3-sidenav")[0].style.display = "block";
document.getElementsByClassName("w3-sidenav")[0].style.opacity = "1";
}
function w3_closeprof() {
document.getElementsByClassName("w3-sidenav")[0].style.display = "none";
}
</script>
答案 0 :(得分:0)
我建议只需制作一个功能就可以简化它:
function w3_open(elNum) {
var el = document.getElementsByClassName("w3-sidenav")[elNum];
el.style.width = "20%";
el.style.textAlign = "center";
el.style.fontSize = "30px";
el.style.paddingTop = "20%";
el.style.display = "block";
el.style.opacity = "1";
}
function w3_close(elNum) {
document.getElementsByClassName("w3-sidenav")[elNum].style.display = "none";
}
然后你会把它称为:
<a title="Personal Information" onclick="w3_open(0)" class="w3-btn-floating w3-card-8 w3-animate-bottom w3-ripple w3-theme w3-red"><i class="fa fa-plus"></i></a>
以w3_open(0)
定位第一个导航,w3_open(1)
定位第二个导航。
与关闭功能相同。
你可以通过CSS类来使它变得更简单:
function w3_toggle(elNum) {
document.getElementsByClassName("w3-sidenav")[elNum].classList.toggle("w3_open");
}
.w3_sidenav.w3_open{
width:20%;
text-align:center;
font-size:30px;
padding-top:20%;
display:block;
opacity:1;
}
然后你只需要一个功能,你只需要调用它w3_toggle(0)
来切换导航打开/关闭。
答案 1 :(得分:0)
获得提示将第二个脚本更改为:
<script>
function w3_openprof() {
document.getElementsByClassName("w3-sidenav")[1].style.width = "20%";
document.getElementsByClassName("w3-sidenav")[1].style.textAlign = "center";
document.getElementsByClassName("w3-sidenav")[1].style.fontSize = "30px";
document.getElementsByClassName("w3-sidenav")[1].style.paddingTop = "20%";
document.getElementsByClassName("w3-sidenav")[1].style.display = "block";
document.getElementsByClassName("w3-sidenav")[1].style.opacity = "1";
}
function w3_closeprof() {
document.getElementsByClassName("w3-sidenav")[1].style.display = "none";
}
</script>
效果很好!
答案 2 :(得分:-1)
尝试使用getElementsById而不是getElementsByClassName