简体   繁体   English

如何使用javascript检查元素是否具有类

[英]How to check if an element has a class using javascript

I'm attempting to use javascript to determine whether an element has a class and then remove it, but I can't seem to get it to work.我正在尝试使用 javascript 来确定一个元素是否有一个类,然后将其删除,但我似乎无法让它工作。

the class name show has the property display: block;类名show具有属性display: block;

 var drop = document.getElementsByClassName('dropdown'); var link = document.getElementsByClassName('faculties') var i for (let i = 0; i < link.length; i++) { link[i].addEventListener('click', () => { var present = drop.classList.contains('show'); if (present == true) { // drop.classList.remove('show') alert('hi') drop[i].classList.toggle("show"); }); }
 <div class="right programmes"> <div class="widget"> <h1>Our Programmes and Courses</h1> </div> <div class="widget"> <div class="faculties"> <a>Faculty of Physical Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown"> <h1>Faculty Stuff</h1> </div> </div> <div class="faculties"> <a>Faculty of Biological Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown"> <h1>Faculty Stuff</h1> </div> </div> <div class="faculties"> <a>Faculty of Health Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown"> <h1>Faculty Stuff</h1> </div> </div> </div> </div>

You are trying to work off a HTML Collection and it does not work that way.您正在尝试处理 HTML 集合,但它不能那样工作。 You need to either loop over the collection or find the element with the class.您需要循环遍历集合或找到具有该类的元素。

Loop环形

for (var j=0; j < drop.length; j++) {
  if(j!==i) drop[j].classList.remove("show");
}
drop[i].classList.toggle('show');

Select选择

// Select the element with a new selector
const active = document.querySelector(".programmes .dropdown.show");
// Make sure it is not the current thing you clicked on
if (active && active !== drop[i]) {
  active.classList.remove('show');
}
drop[i].classList.toggle('show');

Use hidden here (and add that as a boolean attribute on your HTML to reflect initial state):在此处使用hidden (并将其添加为 HTML 上的布尔属性以反映初始状态):

 const faculties = document.querySelectorAll('.faculties'); const dropdowns = document.querySelectorAll('.dropdown'); for (const faculty of faculties) { faculty.addEventListener('click', e => { for (const dropdown of dropdowns) { dropdown.hidden = ![...faculty.children].includes(dropdown); } }) }
 <div class="right programmes"> <div class="widget"> <h1>Our Programmes and Courses</h1> </div> <div class="widget"> <div class="faculties"> <a>Faculty of Physical Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown" hidden> <h1>Faculty Stuff</h1> </div> </div> <div class="faculties"> <a>Faculty of Biological Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown" hidden> <h1>Faculty Stuff</h1> </div> </div> <div class="faculties"> <a>Faculty of Health Sciences <i class="fas fa-chevron-down"></i></a> <div class="dropdown" hidden> <h1>Faculty Stuff</h1> </div> </div> </div> </div>

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

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