gpt4 book ai didi

jquery - 显示/隐藏多个元素

转载 作者:行者123 更新时间:2023-12-01 08:12:53 25 4
gpt4 key购买 nike

必须根据大量逻辑检查来控制大量 dom 元素的可见性 - 我真的希望在 jquery 中实现这样的功能:

$('.FirstPanel').visible = (condition == 1 || othercondition == true);
$('.SecondPanel').visible = (condition > 3 || othercondition == false);
$('.ThirdPanel').visible = (condition < 3 || stillanothercondition = 'asdf');
etc

当然,我可以使用 if 或 switch 语句并调用 $('.FirstPanel').hide() 来表达上面的代码...但这需要很多倍的行数代码。

if(condition == 1 || othercondition == true) {
$('.FirstPanel').show();
$('.SecondPanel').hide();
$('.ThirdPanel').hide();
} else if (condition > 3 || othercondition == false) {
$('.FirstPanel').hide();
$('.SecondPanel').show();
$('.ThirdPanel').hide();
} etc

我觉得我错过了一些明显的东西。谢谢

<小时/>

2012.09.24更新

感谢大家的回答 - 切换方法就是票证(请参阅下面 Michaël Witrant 接受的答案)

最佳答案

$('.FirstPanel').toggle(condition == 1 || othercondition == true);
$('.SecondPanel').toggle(condition > 3 || othercondition == false);
$('.ThirdPanel').toggle(condition < 3 || stillanothercondition = 'asdf');

http://api.jquery.com/toggle/

关于jquery - 显示/隐藏多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12411996/

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