Use vanilla javascript to add / remove class to a different element after clicking on another one

本秂侑毒 提交于 2019-11-29 12:55:37

问题


I have looked through a number of similar questions but can not find a specific example of one that answers in vanilla JS how to add and remove a class to a different element from the one clicked on. I know it has something to do with setting up a loop and iterating through the elements, but I got lost in the exact process.

I have a number of elements with a class name of faq-container and when I click on any of them, I would like the class faq-display added to the body tag. I know I have to set up a loop like for (var i = 0; i < elements.length; i++) { elements[i].classList.remove('hover'); } but I am unsure as to exactly where to write it in the code to make this work. I have tried a number of ways but all fail.

My current script is as follows, where I simply target the first element in the array, but I want to be able to click on any of the faq-container elements and add the class name to the first and only body tag:

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

  document.getElementsByClassName('faq-container')[0].addEventListener('click', function() {
    var faqToggle = document.getElementsByTagName('body')[0];
    if (faqToggle.classList.contains('faq-display')) {
      faqToggle.classList.remove('faq-display');
      // alert("remove faq display!");
    } else {
      faqToggle.classList.add('faq-display');
      // alert("add faq display!");
    }
  });


});
<div class="faq-container cf" id="faq-container">
  <h3 <?=ifxless::element( 'name')?>><?=ifxless::fill($this,'name');?> </h3>

  <div class="faq-content">
    <div class="h_line">&nbsp;</div>

    <div class="faq-bullets" <?=ifxless::element( 'content')?>>
      <?=ifxless::fill($this, 'content');?>
    </div>

  </div>

</div>

回答1:


document.addEventListener("DOMContentLoaded", function() {
  var faqContainers = document.getElementsByClassName('faq-container');
  var faqToggle = document.getElementsByTagName('body')[0];
  for (var i = 0; i < faqContainers.length; i++) {

    faqContainers[i].addEventListener('click', function() {

      if (faqToggle.classList.contains('faq-display')) {
        faqToggle.classList.remove('faq-display');
        // alert("remove faq display!");
      } else {
        faqToggle.classList.add('faq-display');
        // alert("add faq display!");
      }

    });
  }


});



回答2:


You need to loop:

var faqs = document.querySelectorAll('.faq-container');
for (var i=0;i<faqs.length;i++) {
  faq[i].addEventListener('click', toggleFaq);
  // or faq[i].onclick=toggleFaq;
}


function toggleFaq() {
  var faqToggle = document.getElementsByTagName('body')[0];
  if (faqToggle.classList.contains('faq-display')) {
    faqToggle.classList.remove('faq-display');
    // alert("remove faq display!");
  } else {
    faqToggle.classList.add('faq-display');
    // alert("add faq display!");
  }
}

NOTE: if you have MANY of these divs, you are better off having one event handler for a parent and test what was clicked. I also gave you a crossbrowser attach event handler

function addEvent(element, myEvent, fnc) { return ((element.attachEvent) ? element.attachEvent('on' + myEvent, fnc) : element.addEventListener(myEvent, fnc, false)); }

var topContainer = document.getElementById('faqContainerParent');

addEvent(topContainer,'click', function(e) {
  var tgt = e.target;
  if (tgt && tgt.nodeName === 'DIV') {
    if (tgt.classList.contains("faq-container")) {
      toggleFaq();
    }
  }
}


来源:https://stackoverflow.com/questions/41550585/use-vanilla-javascript-to-add-remove-class-to-a-different-element-after-clicki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!