gpt4 book ai didi

HTML 切换显示和隐藏

转载 作者:行者123 更新时间:2023-11-28 15:46:51 24 4
gpt4 key购买 nike

我开发了一个带有侧边栏标题的 HTML 文档,我想在其中单击标题,它必须显示和隐藏标题下方的组件。

问题是它只适用于我的“登录”标题。对于我的“什么是出勤”标题,它没有显示和隐藏其下方的组件,它隐藏了“登录”组件。所以根据我的说法,它不会对“什么是出勤”标题生效。

我还想在每次打开文档时隐藏组件。

另一个问题。每次显示标题下的组件时,如何使箭头旋转?

这是我目前所拥有的:

HTML

<script>
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>

<body style=background-image:url('Images/blue.jpg')>

<section id="body" class="width" >


<aside id="sidebar" class="column-left">
<img class="thumbnail" src="Images/VsoftLogo.jpg" alt="thumbnail" />

<h2 onclick="myFunction()" class ="sidebar-heading"><i class="down" style-"top:-50px"></i> LOGINS</h2>
<div id="myDIV">
<nav id="mainnav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Solutions</a></li>
<li><a href="#">Contact1</a></li>
<li><a href="#">Contact1</a></li>
<li><a href="#">Contact1</a></li>

</ul>
</nav>


</div>


<h4 onclick="myFunction()" class ="sidebar-heading"><i class="down" style-"top:-50px"></i> what is attendance</h4>
<div id="myDIV">
<nav id="mainnav">
<ul>
<li><a href="#">Example</a></li>
<li><a href="#">Example2</a></li>
<li><a href="#">Example3</a></li>
<li><a href="#">Example4</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Contact1</a></li>
<hr>
</ul>
</nav>
</div>

CSS

.mystyle {
width: 100%;
padding: 25px;
background-color: coral;
color: white;
font-size: 25px;
}
i {
border: solid #ffb200;
border-width: 0 5px 5px 0;
display: inline-block;
padding: 5px;

}
.down {
color:#ffb200;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
}

最佳答案

在这里,您为多个容器使用了一个 id。 DOM 中的 id 在任何时候都应该是唯一的。

此外,HTML 中的 style 标签存在语法错误。

您可以使用 event 属性并相应地操作它以在页面加载时默认隐藏,而不是在此处定义 id。

您可以将类 mainnav 分配给您的显示/隐藏容器,使它们成为 h

检查 fiddle .

引用代码:

function myFunction(event) {
var x = event.target.nextElementSibling,
y = event.target.firstChild;
if (x.style.display === 'none' || x.style.display === '') {
x.style.display = 'block';
y.classList.add("down");
y.classList.remove("right");
} else {
x.style.display = 'none';
y.classList.remove("down");
y.classList.add("right");
}
}
.sidebar-heading {
cursor: pointer;
}

.mystyle {
width: 100%;
padding: 25px;
background-color: coral;
color: white;
font-size: 25px;
}

i {
border: solid #ffb200;
border-width: 0 5px 5px 0;
display: inline-block;
padding: 5px;
}

.down {
color: #ffb200;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
}

.right {
color: #ffb200;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
}

.mainnav {
display: none;
}
<body>
<section id="body" class="width">
<aside id="sidebar" class="column-left">
<h2 onclick="myFunction(event);" class="sidebar-heading"><i class="right" style="top:-50px"></i> LOGINS</h2>
<div class="mainnav">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Solutions</a></li>
<li><a href="#">Contact1</a></li>
<li><a href="#">Contact1</a></li>
<li><a href="#">Contact1</a></li>
</ul>
</nav>
</div>


<h4 onclick="myFunction(event);" class="sidebar-heading"><i class="right" style="top:-50px"></i> what is attendance</h4>
<div class="mainnav">
<nav>
<ul>
<li><a href="#">Example</a></li>
<li><a href="#">Example2</a></li>
<li><a href="#">Example3</a></li>
<li><a href="#">Example4</a></li>
<li><a href="#">Example</a></li>
<li><a href="#">Contact1</a></li>
<hr>
</ul>
</nav>
</div>

关于HTML 切换显示和隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526534/

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