gpt4 book ai didi

javascript - 有没有更简洁的方法来隐藏和显示 div 与 Javascript?

转载 作者:行者123 更新时间:2023-12-05 00:30:02 24 4
gpt4 key购买 nike

我正在创建一个仪表板,其中包含大约 20 个以“display: none;”开头的 div。
当使用侧边栏中的 .onClick() 时,它将显示特定的 div 并隐藏所有其他 div。
我使用了为每个 div 创建一个函数的经典解决方案,但是非常冗长,代码看起来很乱。
有没有更简洁的方法来使用 Javascript 实现这一点?
这是我的代码:

function presale() {
var x = document.getElementById("presale");
var y = document.getElementById("claim");
var z = document.getElementById("stake");
if (x.style.display === "grid") {
x.style.display = "none";
} else {
x.style.display = "grid";
y.style.display = "none";
z.style.display = "none";
}
}

function claim() {
var x = document.getElementById("presale");
var y = document.getElementById("claim");
var z = document.getElementById("stake");
if (y.style.display === "grid") {
y.style.display = "none";
} else {
x.style.display = "none";
y.style.display = "grid";
z.style.display = "none";
}
}

function stake() {
var x = document.getElementById("presale");
var y = document.getElementById("claim");
var z = document.getElementById("stake");
if (z.style.display === "grid") {
z.style.display = "none";
} else {
x.style.display = "none";
y.style.display = "none";
z.style.display = "grid";
}
}
*,
html {
color: #fff;
background-color: black;
}

#presale,
#claim,
#stake
/* Here I have many other divs like below */

{
display: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<link rel="stylesheet" href="MOD.CSS">
<script src="main2.js"></script>
<title>Base Template</title>
</head>

<body>
<div>
<ul>
<!-- Here I have other 20 options like the above -->
<li onclick="presale()">Presale</li>
<li onclick="claim()">Claim</li>
<li onclick="stake()">Stake</li>
<!-- Here I have other 20 options like the above -->
</ul>
<div id="presale">
<h1>Presale</h1>
</div>
<div id="claim">
<h1>Claim</h1>
</div>
<div id="stake">
<h1>Stake</h1>
</div>
</div>
</body>

</html>

有没有更好的方法来做到这一点,而无需创建一个函数并为每个 div 一遍又一遍地重复相同的事情?

最佳答案

根本不需要JS。您可以简单地使用 anchor 并使用 #id作为超引用。然后您可以使用 :target 通过 CSS 显示元素-选择器:

*,
html {
color: #fff;
background-color: black;
}

.d-none
/* Here I have many other divs like below */

{
display: none;
}

div:target {
display: grid;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<link rel="stylesheet" href="MOD.CSS">
<script src="main2.js"></script>
<title>Base Template</title>
</head>

<body>
<div>
<ul>
<!-- Here I have other 20 options like the above -->
<li><a href ="#presale">Presale</a></li>
<li><a href ="#claim">Claim</a></li>
<li><a href ="#stake">Stake</a></li>
<!-- Here I have other 20 options like the above -->
</ul>
<div id="presale" class="d-none">
<h1>Presale</h1>
</div>
<div id="claim" class="d-none">
<h1>Claim</h1>
</div>
<div id="stake" class="d-none">
<h1>Stake</h1>
</div>
</div>
</body>

</html>

关于javascript - 有没有更简洁的方法来隐藏和显示 div 与 Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72617531/

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