隐藏和amp;是否有一种更短的简洁方法;与JavaScript一起表演?
我正在创建一个大约20个DIV的仪表板,以“ Display:None;”开头。
当使用侧边栏中的.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>
是否有更好的方法可以做到这一点,而无需创建一个函数并一遍又一遍地重复同一件事?
I am creating a dashboard with approximately 20 divs starting with "display: none;".
When the .onClick() in the sidebar will be used, it will show a specific div and keep hidden all the others.
I have used the classic solution of creating a function for each div, however, is extremely lengthy and the code looks like a mess.
Is there a better cleaner way to achieve this with Javascript?
Here is my code:
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>
Is there a better way to do this without the need to create a function and repeat the same thing over and over for each div?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
根本不需要JS。您可以简单地使用锚点,然后使用
#id
作为超级参考。然后,您可以通过使用:target
-Selector通过CSS显示元素:There is no need for JS at all. You can simply use an anchor and use
#id
as hyper reference. Then you can display the element through CSS by using the:target
-selector:在这里,您可以看到一种香草JavaScript解决方案。
content
divs默认是隐藏的。如果单击一个元素,则对应
data-id
获取类show
。Here you see a vanilla Javascript solution.
content
divs are by default hidden.If you click an element, the corresponding
data-id
get the classshow
.这样的使用数据属性和类列表切换也应起作用。
我将考虑通过使用通用CSS选择器隐藏/显示各个部分来最大程度地降低您的代码(和CSS)。这也使下一个人更容易可伸缩性和可维护性。
这具有使用CSS 100%控制的样式的额外好处,而不是JavaScript设定的任意内联样式。
添加另一个部分也很容易:
AwesomeSection
)data-toggle section
将ID作为值&lt; li data-toggle section =“很棒的
”事件侦听器使用
[Data-Toggle section]
选择器绑定,这基本上可以显示或隐藏一个部分,只要它具有具有正确值的属性即可。Something like this using data attributes and classlist toggles should also work.
I would consider minimizing your code (and CSS) by using generic CSS selectors to hide/show the individual sections. This also makes scalability and maintainability easier for the next guy.
This has the added benefit of your styling being controlled 100% using CSS and not arbitrary inline styles set by the javascript.
Adding another section is also easy as can be:
awesome-section
)data-toggle-section
with the id as the value<li data-toggle-section="awesome-section">Awesome Section</li>
You're also not restricted to using just the nav elements themselves as the event listener is bound using the
[data-toggle-section]
selector which means that basically anything can show or hide a section as long as it has that attribute with the correct value.您可以简单地将同一类( eg
my_div
)分配给每个可显示的div
,然后将id
传递给您的功能(这将显示并隐藏所有其他功能)。You could simply assign the same class (e.g.
my_div
) to every showablediv
, then pass theid
to your function (that will show that and hide all the others).这是我的尝试。它与 @ztom的答案明显相同,但我尝试避免使用
foreach
。Here's my attempt. It's sensibly the same as @ztom's answer but I tryed avoiding a
foreach
.当涉及到多个元素上使用相同的逻辑时,使用类而不是ID,并且默认情况下缩短了解决方案。
使用
jQuery
,它基本上是一个2栏:在CSS中,使用
display创建类
.hidden
display:none;您的
div
和li
元素也应使用类分组。然后,您可以简单地参考此类并添加节目/隐藏逻辑:
When it comes to use the same logic on multiple elements, use classes instead of id's and your solution is shortened by default.
With
jQuery
, it's basically a 2-liner:in CSS, create a class
.hidden
withdisplay:none;
Your
div
andli
elements should be grouped, using a class too.Then you can simply refer to this classes and add the show/hide logic by:
这是代码审查部分的问题 https://codereview.stackexchange.com/
但是,您可以尝试这样的smth
。 html将elem名称添加为一个参数,所以,
用此替换为
This is a question for Code Review section https://codereview.stackexchange.com/
However, you can try smth like this:
and in html add the elem name as a param, so, replace this
with this
如果您附加
附加文档
/a>
匹配
queryselector
querySelectorall
P>If you attach data attributes to both the list items and the "panels" you can use one function to match them up, and use a CSS class to determine whether it should be active or not.
Additional documentation
classList
Destructuring assignment
Event delegation
matches
querySelector
/querySelectorAll
Template/string literals