Get next element with specific class of clicked element with pure js

前端 未结 3 2081
深忆病人
深忆病人 2021-01-24 04:06

I am trying to get the the first element with a specific class which follow the element clicked with pure JS (no JQuery) in the following way but get el.nextSibling is not a fun

3条回答
  •  臣服心动
    2021-01-24 04:36

    nextElement is a property, not a function, so you don't use () with it. That said, using nextSibling can give you white space content which you don't want. Instead you can use nextElementSibling:

    el.nextElementSibling.style.display = 'block';
    

    const togglers = document.querySelectorAll('.toggler');
    //console.log(togglers);
    togglers.forEach(function(el) {
      el.addEventListener('click', function(e) {
        //const content = el.innerHTML;
        //console.log(content);
        el.nextElementSibling.style.display = 'block';
      })
    });
    Click me 1
    Click me 2

提交回复
热议问题