简体   繁体   English

我有一个关于显示/隐藏 function 的问题

[英]I have a question about show/hide function

I have a question about show/hide function with divs that have multiple classes我有一个关于使用具有多个类的 div 显示/隐藏 function 的问题

So I have 4 buttons and 4 classes, and I know how to show/hide divs that have only one class...所以我有 4 个按钮和 4 个类,我知道如何显示/隐藏只有一个 class 的 div...

<button id="button1"></button>
<button id="button2"></button>
<button id="button3"></button>
<button id="button4"></button>

<div class="class1"></div>
<div class="class2"></div>
<div class="class3"></div>
<div class="class4"></div>

However, if div has multiple classes:但是,如果 div 有多个类:

<div class="class1 class2"></div>

following script doesn't work..以下脚本不起作用..

<script>
$(document).ready(function(){
$("#button1").click(function(){
$(".class1").show();
$(".class2").hide();
$(".class3").hide();
$(".class4").hide();
});
$("#button1").click(function(){
$(".class1").hide();
$(".class2").show();
$(".class3").hide();
$(".class4").hide();
});
$("#button3").click(function(){
$(".class1").hide();
$(".class2").hide();
$(".class3").show();
$(".class4").hide();
});
$("#button4").click(function(){
$(".class1").hide();
$(".class2").hide();
$(".class3").hide();
$(".class4").show();
});
</script>

Also, can I write it somehow that I don't have to write every div that I want to hide?另外,我可以以某种方式编写它,而不必编写我想隐藏的每个 div 吗?

.hasClass show()
else hide()

Because in the future I might have 20 classes or more...因为将来我可能有20节课或更多......

Sorry I am beginner:)对不起,我是初学者:)

It is because you the show first and then hide .那是因为你先showhide In order for it to work, you should hide all class name selectors and then do a .show for the relevant class selector.为了让它工作,您应该隐藏所有 class 名称选择器,然后为相关的 class 选择器执行.show

A cleaner approach would be create a hideAll method and then .show for the relevant class selector.更简洁的方法是创建hideAll方法,然后为相关的.show选择器创建 .show。

it think it's every think work ok but you have a syntax error in last and that who stop everything它认为这一切都很好,但是你最后有一个语法错误,并且停止了一切

$(document).ready(function(){
    $("#button1").click(function(){
    $(".class1").show();
    $(".class2").hide();
    $(".class3").hide();
    $(".class4").hide();
    });
    $("#button2").click(function(){
    $(".class1").hide();
    $(".class2").show();
    $(".class3").hide();
    $(".class4").hide();
    });
    $("#button3").click(function(){
    $(".class1").hide();
    $(".class2").hide();
    $(".class3").show();
    $(".class4").hide();
    });
    $("#button4").click(function(){
    $(".class1").hide();
    $(".class2").hide();
    $(".class3").hide();
    $(".class4").show();
    })
    
});

I got the answer!我得到了答案!

Hide all divs first and then show what you want!先隐藏所有 div,然后显示你想要的!

Like this, divs can have multiple classes!像这样,div 可以有多个类!

<button id="button1"></button>
<button id="button2"></button>
<button id="button3"></button>
<button id="button4"></button>

<div class="class1"></div>
<div class="class2"></div>
<div class="class3"></div>
<div class="class4"></div>
<div class="class1 class2"></div>
<div class="class3 class4"></div>
<div class="class1 class2 class3 class4"></div>

<script>
$(document).ready(function(){
$("#button1").click(function(){
$("div").hide();
$(".class1").show();
});
$("#button1").click(function(){
$("div").hide();
$(".class2").show();
});
$("#button1").click(function(){
$("div").hide();
$(".class3").show();
});
$("#button1").click(function(){
$("div").hide();
$(".class4").show();
});
</script>

Thanks to everybody!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM