2015-10-15 83 views
0

我试图让两个独立的导航菜单独立运作,取决于用户点击哪个按钮,并显示在同一个地方(左)。我有2个独立的功能,但只能得到两个按钮来打开相同的导航菜单(链接1,2,3不是链接4,5,6)。我可以打开第二个菜单,但前提是将ClassName更改为topnav而不是sidenav。我也尝试getElementByid,但它也不起作用。感谢您的任何帮助。有2个独立的<nav>标签独立工作

<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 &times;</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 &times;</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)来切换导航开启/关闭。

+0

这是一个更清洁,运作良好。再次感谢 – AL86

+0

没问题:D很高兴我能帮到 –

-1

尝试使用getElementsById代替getElementsByClassName方法

+0

这就是'getElementById',fyi – CollinD

+0

不,那不是真正的答案。 –

+0

你好!对不起.... CollinD是正确的:) –

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> 

的伟大工程!

+1

大声笑,我删除了答案,因为这并不真正工作最好的,非常混乱:D –