gpt4 book ai didi

javascript - 单击时的 CSS 列表垂直菜单

转载 作者:行者123 更新时间:2023-12-03 04:34:39 25 4
gpt4 key购买 nike

我对 JavaScript 很陌生。我设置了一个包含 2 个 iframe 的页面。

左侧 iframe 中是垂直菜单,右侧 iframe 中是单击列表项时要显示的内容。

内容已成功加载到框架中,但我试图在单击列表项时更改其背景颜色。

下面的代码与我对列表中前两项的预期相同。但是,当我单击“成员搜索”时,将使用默认情况,并且该按钮下面的任何按钮都不会改变颜色。

有谁知道为什么吗?

<html>
<head>
<meta charset="UTF-8">
<script>
function menuClicked(button) {

var Button = button;

switch (Button) {

case "queue":

document.getElementById("queueButton").className = "active";
document.getElementById("tickerButton").className = "notactive";
document.getElementById("memberButton").className = "notactive";
document.getElementById("staffButton").className = "notactive";
document.getElementById("settingsButton").className = "notactive";
document.getElementById("reportsButton").className = "notactive";
document.getElementById("logoutButton").className = "notactive";
break;

case "ticker":

document.getElementById("queueButton").className = "notactive";
document.getElementById("tickerButton").className = "active";
document.getElementById("memberButton").className = "notactive";
document.getElementById("staffButton").className = "notactive";
document.getElementById("settingsButton").className = "notactive";
document.getElementById("reportsButton").className = "notactive";
document.getElementById("logoutButton").className = "notactive";
break;

case "member":


document.getElementById("queueButton").className = "notactive";
document.getElementById("tickerButton").className = "notactive";
document.getElementById("memberButton").className = "active";
document.getElementById("staffButton").className = "notactive";
document.getElementById("settingsButton").className = "notactive";
document.getElementById("reportsButton").className = "notactive";
document.getElementById("logoutButton").className = "notactive";
break;

default:
document.getElementById("queueButton").className = "active";
document.getElementById("tickerButton").className = "notactive";
document.getElementById("memberButton111").className = "notactive";
document.getElementById("staffButton").className = "notactive";
document.getElementById("settingsButton").className = "notactive";
document.getElementById("reportsButton").className = "notactive";
document.getElementById("logoutButton").className = "notactive";
break;
}


}
</script>
<style>
body {
margin: 0;
}


ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 100%;
background-color: #373946;
position: fixed;
height: 80%;
overflow: auto;
}

li a {
display: block;
color: #6b6b6b;
padding: 8px 16px;
text-decoration: none;
font-family: "Helvetica";
}

li a.active {
background-color: #dd3a78;
color: white;
}

li a:hover:not(.active) {
background-color: #555;
color: white;
}



div {
height: 20%;
width: 100%;
background-color: #373946;
display:flex;
align-items:center;
justify-content:center;
}

</style>
</head>
<body>

<div id="queue" style=" vertical-align: middle;">
<img src="/sms/images/asmlogo.png" height="100" width="100" >
</div>

<ul>
<li><a id="queueButton" href="/sms/frames/piles2.php" onclick="menuClicked('queue')" STYLE="text-decoration: none" target="main">Message Queue</a></li>
<li><a id="tickerButton" href="/sms/frames/ticker2.php" onclick="menuClicked('ticker')" STYLE="text-decoration: none" target="main">Message Ticker</a></li>
<li><a id="memberButton" href="/sms/frames/piles3.php" onclick="menuClicked('member')" STYLE="text-decoration: none" target="main">Member Search</a></li>
<li><a id="staffButton" href="/sms/frames/ticker2.php" onclick="menuClicked('staff')" STYLE="text-decoration: none" target="main">Staff</a></li>
<li><a id="settingsButton" href="/sms/frames/ticker2.php" onclick="menuClicked('settings')" STYLE="text-decoration: none" target="main">Settings</a></li>
<li><a id="reportsButton" href="/sms/frames/ticker2.php" onclick="menuClicked('reports')" STYLE="text-decoration: none" target="main">Reports</a></li>
<li><a id="logoutButton" href="/sms/frames/ticker2.php" onclick="menuClicked('logout')" STYLE="text-decoration: none" target="main">Log Out</a></li>
</ul>
</body>
</html>

编辑:

此链接上的代码:

https://www.asmserver.co.uk/sms/frames/menu2.html

最佳答案

你的 JavaScript 看起来很不错,虽然很长,但从我从你的预览链接中可以看出,你的类名之前的不间断空格似乎有些奇怪? I've tested it in Chrome's devtools (see attached) - 第一个 JS 调用使用从 JavaScript 复制的代码,第二个调用是通过从 HTML 元素复制类名来完成的。

看起来确实是一件很奇怪的事情!不确定它们是如何包含在其中的 - 可能值得返回并重写每个元素 ID。它似乎也会影响 staffButtonreportsButtonlogoutButton

希望这有帮助!

关于javascript - 单击时的 CSS 列表垂直菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com