简体   繁体   English

当我们通过点击事件切换另一个班级时,如何删除以前切换的班级

[英]How to remove a previously toggled class when we toggle another class by click event

I dont use Jquery and dont want to , I wanted to toggle a class again once next item is toggled with same class. 我不使用Jquery,也不想这样做,我想在下一个项目与同一个类别切换后再次切换一个类别。 so the class from previous one is removed. 因此,上一个课程将被删除。 and only one class is toggled on at a time. 并且一次只打开一个类。

I tried adding document.getElementsByClassName(".panel").removeClass("open"); 我尝试添加document.getElementsByClassName(".panel").removeClass("open"); to the function to first remove all toggled class('open') but that doesnt work. 到该功能,以首先删除所有已切换的类(“打开”),但这不起作用。 i tried adding an id and trying to remove a class from that id still doesn't work 我试图添加一个ID并尝试从该ID中删除一个类仍然无法正常工作

function toggleOpen() {

     document.getElementsByClassName(".panel").removeClass("open");//this give me error
      this.classList.toggle('open');


    }
    function toggleActive(e) {
      if (e.propertyName.includes('flex')) {
        this.classList.toggle('open-active');
      }

    }



    const panels = document.querySelectorAll('.panel');
    panels.forEach(panel => panel.addEventListener('click', toggleOpen));
    panels.forEach(panel => panel.addEventListener('transitionend', toggleActive));

Result 结果

Uncaught TypeError: document.getElementsByClassName(...).removeClass is not a function

It should be classList.remove("open") , not removeClass("open") 它应该是classList.remove("open") ,而不是removeClass("open")

Also, it wouldn't work because getElementsByClassName would return a collection of elements, and you can't remove the class from them all at once. 同样,它也行不通,因为getElementsByClassName将返回元素的集合,并且您无法一次从所有元素中删除该类。 You'd have to call it for each element in the panel. 您必须为面板中的每个元素调用它。

Array.from(document.getElementsByClassName(".panel")).forEach(element => element.classList.remove("open"))

Or without turning it to an array first you can do 或者不先将其变成数组就可以

const panels = document.getElementsByClassName(".panel")
for (let panel of panels) {
  panel.classList.remove("open")
}

Since you are adding event listener to each element, document.getElementsByClassName(".panel").removeClass("open"); 由于您要将事件侦听器添加到每个元素,因此document.getElementsByClassName(".panel").removeClass("open"); is not required. 不需要。 this inside the function will refer to clicked element. 函数内部的this表示被单击的元素。

removeClass is jquery method, which will not be available. removeClass是jquery方法,将不可用。 document.getElementsByClassName returns list of elements, so you need to loop over it. document.getElementsByClassName返回元素列表,因此您需要对其进行循环。

Try following example. 请尝试以下示例。

 function toggleColor() { this.classList.toggle('red'); } const panels = document.querySelectorAll('.panel'); panels.forEach(panel => panel.addEventListener('click', toggleColor)); 
 .green { color: green; } .red { color: red; } 
 <p class="panel green">H1<p> <p class="panel red">H2<p> <p class="panel green">H3<p> <p class="panel red">H4<p> 

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

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