首页 > 解决方案 > 如果页面 a 是打开的,是否有一种简单的方法来编写 closePage x,y,z?手风琴菜单

问题描述

JavaScript 新手在这里。当我点击手风琴菜单时,我希望 div 向下展开以显示 x,如果 y 或 p 打开,我希望它关闭那些扩展的菜单命令。

错误在于下面的 if 语句。

这是我对代码的尝试:

function openPage() {
  var x = document.getElementById("accordian.Home"),
  var y = document.getElementById("accordian.Contact"),
  var p = document.getElementById("accordian.Pricing");
  if (x.style.display === "hidden") {
    x.style.display = "block";
  } else {
    y.style.display,p.style.display = "hidden";
  }
  if (y.style.display === "hidden") {
    y.style.display = "block";
  } else {
    x.style.display,p.style.display = "hidden";
  }
  if (p.style.display === "hidden") {
    p.style.display = "block";
  } else {
    x.style.display,y.style.display = "hidden";
  }
}

打开手风琴的原始脚本:

<script>
    var acc = document.getElementsByClassName("accordion");
        var i;

    for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click", function() {
    /* Toggle between adding and removing the "active" class,
    to highlight the button that controls the panel */
    this.classList.toggle("active");

    /* Toggle between hiding and showing the active panel */
    var panel = this.nextElementSibling;
    if (panel.style.display === "block") {
      panel.style.display = "none";
    } else {
      panel.style.display = "block";
    }
  });
} 
</script>

js 文件通过打开的内容页面进行选项卡:

function openPage(evt, pageName) {
  var i, tabcontent, accordion;

  // Get all elements with class="tabcontent" and hide them
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }

  // Get all elements with class="tablinks" and remove the class "active"
  accordion = document.getElementsByClassName("accordion");
  for (i = 0; i < accordion.length; i++) {
    accordion[i].className = accordion[i].className.replace(" active", "");
  }

  // Show the current tab, and add an "active" class to the link that opened the tab
  document.getElementById(pageName).style.display = "block";
  evt.currentTarget.className += " active";
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();


提前致谢!如果这是一个简单的解决方案,或者已经被问了一百万次,非常抱歉。

标签: javascripthtml

解决方案


推荐阅读