简体   繁体   English

使用Java脚本删除类

[英]Removing Class Using Javascript

I'm trying to teach myself a little javascript for project I am working on and just wanted to see if I could get some help. 我正在尝试为自己正在研究的项目使用一些JavaScript,只是想看看我是否能得到一些帮助。 I use 3 different drop down menus and I use the below function to hide one menu when another is clicked. 我使用3个不同的下拉菜单,并使用以下功能在单击另一个菜单时隐藏一个菜单。 It worked 有效

function DropDownMenuNavigation() {
    document.getElementById("DropDownMenuNav").classList.toggle("show");
    document.getElementById('DropDownMenuChart').classList.remove('show');
}

The above code worked well when I had 2 different drop down menus. 当我有2个不同的下拉菜单时,上面的代码运行良好。 But now that I have 3 it doesn't seem to see the 3 line I've added below. 但是,既然我有3条,似乎看不到我在下面添加的3条线。

function DropDownMenuNavigation() {
    document.getElementById("DropDownMenuNav").classList.toggle("show");
    document.getElementById('DropDownMenuChart').classList.remove('show');
    document.getElementById('DropDownMenuCat').classList.remove('show');
}

If I switch the bottom line with the middle line it will regonize that line, I'm guessing there is something wrong with the format I'm writing it in? 如果我将底线和中线切换,它将使该行重新变灰,我猜我写的格式有问题吗? Something tells me I'm not including a separator or something. 某些信息告诉我,我不包括分隔符或其他内容。 Anyways, I know its something small, maybe someone could point it out to me. 无论如何,我知道它很小,也许有人可以向我指出。

EDIT: 编辑:

JAVASCRIPT JAVASCRIPT

<script>
function DropDownMenuNavigation() {
document.getElementById("b2DropDownMenuNav").classList.toggle("show");
document.getElementById("b2DropDownMenuCat").classList.toggle("remove");
document.getElementById("b2DropDownMenuCha").classList.toggle("remove");
}
function DropDownMenuCategory() {
document.getElementById("b2DropDownMenuCat").classList.toggle("show");
document.getElementById("b2DropDownMenuNav").classList.toggle("remove");  
}
function DropDownMenuCharts() {
document.getElementById("b2DropDownMenuCha").classList.toggle("show");
document.getElementById("b2DropDownMenuNav").classList.toggle("remove");  
}
</script>

HTML 的HTML

<div class="dropbtn" style="float: left;">
<button onclick="DropDownMenuNavigation()" class="dropbtn">&#9776; MENU</button>
</div>

<div class="dropbtn" style="float: left;">
<button onclick="DropDownMenuCategory()" class="dropbtn">CATEGORIES</button>
</div>

<div class="dropbtn" style="float: left;">
<button onclick="DropDownMenuCharts()" class="dropbtn">CATEGORIES</button>
</div>

<div class="dropdown">
<div id="b2DropDownMenuCategory" class="dropdown-content">
1
</div>
</div>
<div class="dropdown">
<div id="b2DropDownMenuCharts" class="dropdown-content">
2
</div>
</div>
<div class="dropdown">
<div id="b2DropDownMenuNavigation" class="dropdown-content">
3
</div>
</div>

CSS 的CSS

/* Dropdown Button */
.dropbtn {
background-color: #0066a2;
color: white;
padding: 1px;
font-size: 15px;
font-weight: bold;
border: none;
cursor: pointer;
}
.dropbtn a {
color: #FFFFFF;
text-decoration: none;
font-size: 15px;
font-weight: bold;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
float: left;
position: relative;
display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #0066a2;
min-width: 260px;
max-width: 960px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}

/* Links inside the dropdown  */
.dropdown-content a {
color: #000000;
text-decoration: none;
}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;} 

The code you posted has mismatches between the actual id s of the content and the document.getElementById() lines, but assuming that you correct that, your code does in fact work, but each bit of content just winds up going under the other, so you never see the correct content. 您发布的代码在内容的实际iddocument.getElementById()行之间不匹配,但是假设您对此进行了纠正,那么您的代码确实可以正常工作,但是内容的每一点最终都在另一部分的下面,因此您永远看不到正确的内容。

 function DropDownMenuNavigation() { document.getElementById("b2DropDownMenuNav").classList.toggle("show"); document.getElementById("b2DropDownMenuCat").classList.toggle("remove"); document.getElementById("b2DropDownMenuCha").classList.toggle("remove"); } function DropDownMenuCategory() { document.getElementById("b2DropDownMenuCat").classList.toggle("show"); document.getElementById("b2DropDownMenuNav").classList.toggle("remove"); } function DropDownMenuCharts() { document.getElementById("b2DropDownMenuCha").classList.toggle("show"); document.getElementById("b2DropDownMenuNav").classList.toggle("remove"); } 
 /* Dropdown Button */ .dropbtn { background-color: #0066a2; color: white; padding: 1px; font-size: 15px; font-weight: bold; border: none; cursor: pointer; } .dropbtn a { color: #FFFFFF; text-decoration: none; font-size: 15px; font-weight: bold; } /* The container <div> - needed to position the dropdown content */ .dropdown { float: left; position: relative; display: inline-block; } /* Dropdown Content (Hidden by Default) */ .dropdown-content { display: none; position: absolute; background-color: #0066a2; min-width: 260px; max-width: 960px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } /* Links inside the dropdown */ .dropdown-content a { color: #000000; text-decoration: none; } /* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ .show {display:block;} 
 <div class="dropbtn" style="float: left;"> <button onclick="DropDownMenuNavigation()" class="dropbtn">&#9776; MENU</button> </div> <div class="dropbtn" style="float: left;"> <button onclick="DropDownMenuCategory()" class="dropbtn">CATEGORIES</button> </div> <div class="dropbtn" style="float: left;"> <button onclick="DropDownMenuCharts()" class="dropbtn">CATEGORIES</button> </div> <div class="dropdown"> <div id="b2DropDownMenuCat" class="dropdown-content"> 1 </div> </div> <div class="dropdown"> <div id="b2DropDownMenuCha" class="dropdown-content"> 2 </div> </div> <div class="dropdown"> <div id="b2DropDownMenuNav" class="dropdown-content"> 3 </div> </div> 

But, since you are new to this, it's best not to start off with bad habits, so don't use inline HTML event attributes (ie onclick , etc.), there are many reasons why and you can review them here . 但是,由于您是新手,所以最好不要以坏习惯开始,因此不要使用内联HTML事件属性(即onclick等),有很多原因,您可以在这里进行查看

Next, you have a lot of unneeded HTML and the structure of the HTML should be altered to represent the hierarchy of the content. 接下来,您将拥有许多不需要的HTML,并且应该更改HTML的结构以表示内容的层次结构。

Also, you don't need separate functions for each menu click as trying to keep track of what should be hidden and what should be shown in an ever-increasing list of menu items is not a scaleable result. 此外,您不需要为每次单击菜单都使用单独的功能,因为试图跟踪应该隐藏的内容以及在不断增加的菜单项列表中应该显示的内容并不是可缩放的结果。

When these changes are made, the HTML is much cleaner and less involved and the JavaScript is also much simpler: 进行了这些更改后,HTML变得更整洁,使用更少,JavaScript也更加简单:

 // First, get references to the HTML elements your code will need. // You could get individual references, like this: /* var b2DropDownMenuNav = document.getElementById("b2DropDownMenuNav"); var b2DropDownMenuCat = document.getElementById("b2DropDownMenuCat"); var b2DropDownMenuCha = document.getElementById("b2DropDownMenuCha"); */ // But in your case, a single reference to the collection of menus will do. // We'll also want that collection to be converted to a JavaScript array. var menus = Array.prototype.slice.call(document.querySelectorAll(".dropbtn")); // Now, we can just loop over the array and give the buttons a common function // to perform when they are clicked (no need for multiple functions. menus.forEach(function(menu){ menu.addEventListener("click", function(){ // Hide any currently showing menu content Array.prototype.slice.call(document.querySelectorAll(".dropdown-content")).forEach(function(content){ content.classList.remove("show"); }); // Show the content of the menu that was clicked: menu.querySelector(".dropdown-content").classList.toggle("show"); }); }); 
 /* Dropdown Button */ .dropbtn { background-color: #0066a2; color: white; padding: 1px; font-size: 15px; font-weight: bold; border: none; cursor: pointer; float:left; /* no need to write this inline with the HTML, just put it here */ } .dropbtn a { color: #FFFFFF; text-decoration: none; font-size: 15px; font-weight: bold; } /* The container <div> - needed to position the dropdown content */ /* I can't see any need for this class at all: .dropdown { float: left; position: relative; display: inline-block; } */ /* Dropdown Content (Hidden by Default) */ .dropdown-content { display: none; background-color: #0066a2; min-width: 260px; max-width: 960px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); position: absolute; /* z-index: 1; <-- NOT NEEDED */ } /* Links inside the dropdown */ .dropdown-content a { color: #000000; text-decoration: none; } /* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */ .show { display:block; } 
 <!-- There is no need to nest button elements inside of div elements. Just get rid of the buttons completely and make the divs the clickable elements. --> <div class="dropbtn" id="b2DropDownMenuNav">&#9776; MENU <div class="dropdown dropdown-content">1</div> </div> <div class="dropbtn" id="b2DropDownMenuCat">CATEGORIES <div class="dropdown dropdown-content">2</div> </div> <div class="dropbtn" id="b2DropDownMenuCha">CATEGORIES <div class="dropdown dropdown-content">3</div> </div> 

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

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