在纯 javascript 中一次打开一个菜单的切换菜单

toggle menu with one menu open at a time in pure javascript

我想切换菜单,一次打开菜单中的一项。我可以一次打开一个项目,但无法同时进行切换。

var navclick = document.getElementsByClassName("js-dropdown");
var navContent = document.getElementsByClassName('-arrow-link-content');
for (var i = 0; i < navclick.length; i++) {
    navclick[i].onclick = function () {
      if (this.parentNode.querySelector('div.-arrow-link-content').classList.contains('nav-active')) {
        this.parentNode.querySelector('div.-arrow-link-content').classList.remove('nav-active'); 
      }
      else {
        this.parentNode.querySelector('div.-arrow-link-content').classList.add('nav-active');
      }
    }
  }
.-arrow-link-content {
  display:none;
}
.nav-active{ display:block;}
<ul class="c-icons" id="c-iconslist">
                    <li>
                        <div class="c-icons__text js-dropdown">heading 1</div>
                     <div class="c-icons__textdropdown -arrow-link-content"> Content 1</div>
                    </li>
                       <li>
                        <div class="c-icons__text js-dropdown">heading 2</div>
                     <div class="c-icons__textdropdown -arrow-link-content"> Content 2</div>
                    </li>
                </ul>

如果有人有任何建议..请分享..提前致谢

为了切换工作,我们需要获取所有名称为 class 的节点 -arrow-link-content,然后先删除 nav-active class,然后添加 nav-active 到所选项目。

检查代码段。

var navclick = document.getElementsByClassName("js-dropdown");
var navContent = document.getElementsByClassName('-arrow-link-content');
for (var i = 0; i < navclick.length; i++) {
  navclick[i].onclick = function() {
    if (this.parentNode.querySelector('div.-arrow-link-content').classList.contains('nav-active')) {
      this.parentNode.querySelector('div.-arrow-link-content').classList.remove('nav-active');
      return false
    }


    var el = document.querySelectorAll('div.-arrow-link-content');
    for (var i = 0; i < el.length; i++) {
      if (el[i].classList.contains('nav-active')) el[i].classList.remove('nav-active');
    }
    this.parentNode.querySelector('div.-arrow-link-content').classList.add('nav-active');

  }
}
.-arrow-link-content {
  display: none;
}

.nav-active {
  display: block;
}
<ul class="c-icons" id="c-iconslist">
  <li>
    <div class="c-icons__text js-dropdown">heading 1</div>
    <div class="c-icons__textdropdown -arrow-link-content"> Content 1</div>
  </li>
  <li>
    <div class="c-icons__text js-dropdown">heading 2</div>
    <div class="c-icons__textdropdown -arrow-link-content"> Content 2</div>
  </li>
</ul>

因为一次最多可以打开一个元素,所以我们只需要查找那个元素,如果存在就先关闭它,然后再打开一个未打开的元素

var navclick = document.getElementsByClassName("js-dropdown");
var navContent = document.getElementsByClassName('-arrow-link-content');
for (var i = 0; i < navclick.length; i++) {
    navclick[i].onclick = function () {
      if (this.parentNode.querySelector('div.-arrow-link-content').classList.contains('nav-active')) {
        this.parentNode.querySelector('div.-arrow-link-content').classList.remove('nav-active'); 
      }
      else {
        try {
          // if an open element exists, close it first
          this.parentNode.parentNode.querySelector('.nav-active').classList.remove('nav-active');
         }
          catch (error){ 
            // Error occurs when no open elment exists, in that case: Do nothing
          }
        this.parentNode.querySelector('div.-arrow-link-content').classList.add('nav-active');
      }
    }
  }
 
.-arrow-link-content {
  display:none;
}
.nav-active{ display:block;}
<ul class="c-icons" id="c-iconslist">
                    <li>
                        <div class="c-icons__text js-dropdown">heading 1</div>
                     <div class="c-icons__textdropdown -arrow-link-content"> Content 1</div>
                    </li>
                       <li>
                        <div class="c-icons__text js-dropdown">heading 2</div>
                     <div class="c-icons__textdropdown -arrow-link-content"> Content 2</div>
                    </li>
                </ul>