gpt4 book ai didi

javascript - 下拉导航改变顺序

转载 作者:行者123 更新时间:2023-11-28 19:11:14 25 4
gpt4 key购买 nike

我正在尝试创建一个包含 2 行的下拉导航栏。 默认情况下,仅显示 1 行。如果单击此行,则两行都应显示。然后,如果您单击 2 行中的其中一行,则只会显示该行。等等。这里有几张图片可以展示我希望实现的目标。

这是我正在尝试实现的示例,但我希望在没有 jQuery 的情况下实现。 https://codepen.io/matthewbeta/pen/zioHr

enter image description here enter image description here enter image description here

它已经部分起作用了,但我无法弄清楚如何仅在单击时显示 Row-B。现在发生的是,您单击 Row-A,显示两行。如果您点击 B 行,则仅显示 A 行。

var select = document.getElementById("select");
var rowB = document.getElementById("rowB");
rowB.style.display = "none";

select.addEventListener("click", function(){
if (rowB.style.display == "none") {
rowB.style.display = "flex";
}
else {rowB.style.display = "none"};
});
body {
margin: 0px;
}

#content {
width: 300px;
height: 300px;
background: #000;
display: grid;
grid-template-rows: 70px 160px 70px;
}

#select {
width: 270px;
height: 90px;
margin: 0 auto;
color: #fff;
margin-top: 10px;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
z-index: 2;
}

#rowA,
#rowB {
width: 270px;
height: 40px;
background: #000;
border: #2e2e2e 1px solid;
display: flex;
align-items: center;
z-index: 2;
}
<body>
<div id="content">
<div id="select">
<div id="rowA">Row-A</div>
<div id="rowB">Row-B</div>
</div>
</div>
</body>

最佳答案

仅对js部分进行了一些修改:

var select = document.getElementById("select");
var rowB = document.getElementById("rowB");
rowB.style.display = "none";

select.addEventListener("click", function(){
if(event.target.id === 'rowA') {
rowB.style.display = (rowB.style.display === 'none')? "block" : "none";
rowB.style.order = '1';
rowA.style.order = '0';
}
else if(event.target.id === 'rowB') {
rowA.style.display = (rowA.style.display === 'none')? "block" : "none";
rowA.style.order = '1';
rowB.style.order = '0';
}
});
body {
margin: 0px;
}

#content {
width: 300px;
height: 300px;
background: #000;
display: grid;
grid-template-rows: 70px 160px 70px;
}

#select {
width: 270px;
height: 90px;
margin: 0 auto;
color: #fff;
margin-top: 10px;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
z-index: 2;
}

#rowA,
#rowB {
width: 270px;
height: 40px;
background: #000;
border: #2e2e2e 1px solid;
display: flex;
align-items: center;
z-index: 2;
}
<body>
<div id="content">
<div id="select">
<div id="rowA">Row-A</div>
<div id="rowB">Row-B</div>
</div>
</div>
</body>

关于javascript - 下拉导航改变顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59248943/

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