简体   繁体   English

如何使用javascript显示隐藏/显示菜单?

[英]How to display hide/show menu using javascript?

The code is working but not perfectly.该代码正在运行,但并不完美。


When I click "Test1", the result:当我单击“Test1”时,结果:

test -1测试-1

test -2测试-2


The problem is when I click "Test2"问题是当我点击“Test2”

I am getting same result when I click "Test1"单击“Test1”时得到相同的结果

the result:结果:

test -1测试-1

test -2测试-2

Expected Result:预期结果:

test -3测试-3

test -4测试-4


Here is the HTML:这是 HTML:

 function show() { var x = document.getElementById("showw"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } function expand() { var y = document.getElementById("show-list"); if (y.style.display === "none") { y.style.display = "block"; } else { y.style.display = "none"; } }
 <h1 onclick="show()"> Menu </h1> <nav id="showw" style="display: none;" onclick="expand()"> <nav id="menuu"> Test1 <nav id="show-list" style="display: none;"> <p>test -1</p> <p>test -2</p> </nav> </nav> <nav id="menuu"> Test2 <nav id="show-list" style="display: none;"> <p>test -3</p> <p>test -4</p> </nav> </nav> </nav>

There's a great deal wrong with your code, starting with the fact that you have multiple elements that have the same id .您的代码有很多错误,首先是您有多个具有相同id元素。 IDs must be unique and this is why no matter which menu item you click, you always see the first set of choices - - the system only expects to find one element with a given id , so when it finds it, it stops looking for others. ID 必须是唯一的,这就是为什么无论您单击哪个菜单项,您总是会看到第一组选项——系统只希望找到一个具有给定id元素,因此当它找到它时,它会停止寻找其他元素.

From an HTML standpoint, you are using nav elements incorrectly.从 HTML 的角度来看,您错误地使用了nav元素。 They should just be for sections that contain navigation.它们应该只用于包含导航的部分。

Lastly, your code uses inline event handling and inline styles, both of which should be avoided as it makes the code more unreadable and promotes duplication of code.最后,您的代码使用内联事件处理和内联样式,应避免使用这两种方式,因为这会使代码更难以阅读并促进代码重复。 CSS and JavaScript should be separated out. CSS 和 JavaScript 应该分开。

See comments inline below:请参阅下面的内联评论:

 // Do your event binding in JavaScript, not in HTML document.querySelector("h1").addEventListener("click", show); // Find all the "menu" elements and loop over them document.querySelectorAll(".menu").forEach(function(nav){ // Assign a click event handler to each nav.addEventListener("click", expand); }); function show(){ // Get all the menu elements and loop over them document.querySelectorAll("div.menu").forEach(function(item){ // Remove the "hidden" CSS class on the element, which will // cause it to be shown. No need to set display:block -- // just stop hiding it. item.classList.remove("hidden"); }); } function expand(){ // Get the child nav of the clicked menu and toggle the use of the "hidden" class this.querySelector("nav").classList.toggle("hidden"); }
 /* Use CSS classes instead of inline style attributes. This avoids messy duplication of code and is much easier to apply or remove the classes. */ /* Just add/or remove this class to hide or show an element */ .hidden { display:none; } .pointer { cursor:pointer; }
 <!-- Notice how there is no CSS or JavaScript in the HTML and how much more clean and simple it is to read? Also, see how there are no IDs on anything, just classes to help the JavaScript and CSS know how to find the right elements? --> <h1 class="pointer">Menu</h1> <div> <div class="menu hidden pointer">Test1 <nav class="hidden"> <ul> <li class="pointer">Test - 1</li> <li class="pointer">Test - 2</li> </ul> </nav> </div> <div class="menu hidden pointer">Test2 <nav class="hidden"> <ul> <li class="pointer">Test - 3</li> <li class="pointer">Test - 4</li> </ul> </nav> </div> </div>

Edited to improve answer Here how it is done: 1) You can't assign the same id to two different elements, it should be a class; 编辑以改善答案这里是如何完成的:1)您不能将相同的ID分配给两个不同的元素,它应该是一个类; 2) Add the expand() call to both the navs you want to expand; 2)将expand()调用添加到要扩展的两个导航中; 3) Add "this" as parameter in the call to the function expand(); 3)在函数expand()的调用中添加“ this”作为参数; doing so will allow you to pass the element without manually giving other parameters; 这样一来,您无需手动提供其他参数即可传递元素; 4) in the expand() function accept "element" as parameter and then find the first .show-list from within the nav element you previously clicked and passed. 4)在expand()函数中,接受“ element”作为参数,然后从之前单击并传递的nav元素中找到第一个.show-list。

 function show() { var x = document.getElementById("showw"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } function expand(element) { var y = element.getElementsByClassName("show-list")[0]; if (y.style.display === "none") { y.style.display = "block"; } else { y.style.display = "none"; } } 
 <h1 onclick="show()"> Menu </h1> <nav id="showw" style="display: none;"> <nav id="menuu" onclick="expand(this)"> Test1 <nav class="show-list" style="display: none;"> <p>test -1</p> <p>test -2</p> </nav> </nav> <nav id="menuu" onclick="expand(this)"> Test2 <nav class="show-list" style="display: none;"> <p>test -3</p> <p>test -4</p> </nav> </nav> </nav> 

getElementById() find only the first much so test2 won't be affected. getElementById() 只找到第一个,所以 test2 不会受到影响。 One other option to the already existing answers is to get the target element of the click event and make the changes on it.现有答案的另一种选择是获取单击事件的目标元素并对其进行更改。

change this line:改变这一行:

var y = document.getElementById("show-list");

by this line:通过这一行:

var y = event.target.firstElementChild;

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

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