javascript - 如果页面 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();
提前致谢!如果这是一个简单的解决方案,或者已经被问了一百万次,非常抱歉。
解决方案
推荐阅读
- c# - 如何在 C# Entity Framework Core 中删除树?
- ember.js - 使用两个不同的 ember 构建
- google-cloud-platform - 谷歌云存储在下载公共文件时要求登录
- python - Python如何在不同文件的CSV文件中添加一列?
- java - Hibernate JPA 等于 POJO
- jupyter - 通过浏览器连接到远程jupyter时出现101(重置连接)错误(通过Putty建立的ssh隧道)
- python - 无法从传感器打印值
- node.js - 如何模块化我的 Node.js,express 项目?
- ios - 以编程方式清除 iOS 上的 DNS 缓存
- ios - 复制方法应用:openURL