简体   繁体   English

Vanilla JS - 可以在子元素上添加/删除类,但所有类都处于活动状态

[英]Vanilla JS - Can add/remove class on child elements but all are active

I am able to add and remove the classes on the child elements but all are active.我能够添加和删除子元素上的类,但所有类都处于活动状态。 I am trying to add & remove only on one active element at a time.我试图一次只在一个活动元素上添加和删除。 What am I missing?我错过了什么?

<ul id="parent" class="container">
  <li class="child"><a href="#one">one</a></li>
  <li class="child"><a href="#two">two</a></li>
  <li class="child"><a href="#three">three</a></li>
</ul>


document.addEventListener("DOMContentLoaded", function() {

  const parent = document.querySelector('#parent');
  parent.style.backgroundColor = 'blue';
  const childrens = document.querySelectorAll('.child');
  const child = document.querySelector('.child a')

  Array.prototype.forEach.call(
   document.querySelectorAll('.child'),
    function(element) {
      element.onclick = addActive;
    }
  );

 function addActive(element){
  element = this;
   if(element.classList.contains('active')) {
    element.classList.remove('active');
   } else {
    element.classList.add('active');
   }
 }
});

Here's a: codepen这是一个: codepen

Right now you're adding/removing the active class only from the clicked element.现在,您仅从单击的元素中添加/删除活动类。 You'll have to remove the class from all elements before you set the clicked one as active.在将单击的元素设置为活动之前,您必须从所有元素中删除该类。

eg例如

function addActive(element) {
  element = this;
  if (element.classList.contains('active')) {
    element.classList.remove('active');
  } else {
    childrens.forEach(function(e) {
      e.classList.remove('active');
    });
    element.classList.add('active');
  }
}

One solution is to remove the active class from all elements first:一种解决方案是首先从所有元素中删除active类:

function addActive(element) {

  childrens.forEach(function(elem) {
    elem.classList.remove("active");
  });

  element = this;
  if (element.classList.contains("active")) {
    element.classList.remove("active");
  } else {
    element.classList.add("active");
  }
}

Here is a working Codepen example .这是一个有效的 Codepen 示例

Here maybe the most straight forward way to toggle through your child elements using pure js.这可能是使用纯 js 切换子元素的最直接方式。

<ul id="parent" class="container">
 <li class="child"><a href="#one">one</a></li>
 <li class="child"><a href="#two">two</a></li>
 <li class="child"><a href="#three">three</a></li>
</ul>

<script>
//get children 
var theChildren = document.querySelector('#parent').children;

//set event listener for to each child
function selectChild(child) {
child.addEventListener('click', function() {
    var checkChildStatus = document.querySelector('#parent .selected');
    if (checkChildStatus) {
        checkChildStatus.classList.remove('selected');
    }
    
    child.classList.add('selected');        
 });
}

//loop through all children
[...theChildren].forEach(selectChild);
</script>

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

相关问题 Vanilla JS除了&#39;active&#39;类之外的所有其他元素都删除了类 - Vanilla JS remove class from all other elements besides 'active' class 添加 class 在滚动时处于活动状态。 香草JS - Add class active on scroll. Vanilla JS Vanilla JS,在滚动位置的类上添加和删除 - Vanilla JS, Add and Remove on Class on Scroll position 在原始JavaScript中删除带有特定子级(嵌套至少2个级别)的所有元素 - Remove all elements with a certain child (nested at least 2 levels in), in vanilla JavaScript 如何使用 vanilla JS 将 class.hidden-item 添加到除第一个孩子之外的所有项目 - How to add class .hidden-item to all item except the first child using vanilla JS 列出所有子元素的类名(Vanilla Javascript) - List the class names of all the child elements (Vanilla Javascript) 如果孩子有一个活跃的班级,那么也给父母一个活跃的班级-Vanilla JS - If a child has a class of active then give parent an active class too - Vanilla JS 如何使用 Vanilla JS 选择直接子元素 - How can i select Direct Child elements using Vanilla JS 在香草JS中,如何根据滚动条是否位于顶部来从元素中添加或删除类? - In vanilla JS, how can I add or remove a class from an element based on whether the scroll is at the top? 如果部分在视口中,则向 div 添加/删除类(vanilla JS) - Add/remove class to div if section is in viewport (vanilla JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM