使用jQuery有条件地隐藏div
如果jQuery中的条件为true,如何隐藏div 1并显示div 2 这是我的密码:使用jQuery有条件地隐藏div,jquery,show-hide,Jquery,Show Hide,如果jQuery中的条件为true,如何隐藏div 1并显示div 2 这是我的密码: <div class="div1">Content 1 </div> <div class="div2">Content 2 </div> function(someFunction) { var option = this.config.option; if (someFunction && this.config.optio
<div class="div1">Content 1 </div>
<div class="div2">Content 2 </div>
function(someFunction) {
var option = this.config.option;
if (someFunction && this.config.option == true) {
option = this.config.option;
}
$$('div.div1').each(function(el) {
$(el).hide(); //and add show div2 ?
});
};
内容1
内容2
函数(someFunction){
var option=this.config.option;
if(someFunction&&this.config.option==true){
option=this.config.option;
}
$$('div.div1')。每个(函数(el){
$(el).hide();//并添加show div2?
});
};
感谢您的帮助。正如评论员所提到的,没有必要使用每个()。 这应该做到:
<div class="div1">Content 1 </div>
<div class="div2">Content 2 </div>
function hide1show2() {
$('div.div1').hide();
$('div.div2').show();
}
内容1
内容2
函数hide1show2(){
$('div.div1').hide();
$('div.div2').show();
}
正如评论员提到的,没有必要使用每个()。
这应该做到:
<div class="div1">Content 1 </div>
<div class="div2">Content 2 </div>
function hide1show2() {
$('div.div1').hide();
$('div.div2').show();
}
内容1
内容2
函数hide1show2(){
$('div.div1').hide();
$('div.div2').show();
}
如何使用该功能
$('div.div1').toggle(option);
$('div.div2').toggle(!option);
使用这个函数怎么样
$('div.div1').toggle(option);
$('div.div2').toggle(!option);
内容1
内容2
函数(someFunction){
var option=this.config.option;
if(someFunction&&this.config.option){
option=this.config.option;
}
如果(选项==“是”){
$$('div.div1')。每个(函数(el){
$(el.hide();
})
$$('div.div2')。每个(函数(el){
$(el.show();
})
}
如果(选项==‘否’){
$$('div.div1')。每个(函数(el){
$(el.show();
})
$$('div.div2')。每个(函数(el){
$(el.hide();
})
}
};
内容1
内容2
函数(someFunction){
var option=this.config.option;
if(someFunction&&this.config.option){
option=this.config.option;
}
如果(选项==“是”){
$$('div.div1')。每个(函数(el){
$(el.hide();
})
$$('div.div2')。每个(函数(el){
$(el.show();
})
}
如果(选项==‘否’){
$$('div.div1')。每个(函数(el){
$(el.show();
})
$$('div.div2')。每个(函数(el){
$(el.hide();
})
}
};
为什么要将each()
与div.div1
一起使用?只有一个这样的元素。@BackinaFlash-…即使有更多的元素,.hide()
将处理所有这些元素。嗨@回到Flash中,我使用each(),因为我需要从这个.config.option获取每个值,有些值可能是假的。谢谢3个人真的投票赞成吗?我已经把这个问题重读了6遍,但我仍然不知道他在这里试图实现什么……以及div
的可见性究竟取决于什么条件?您能否解释一下您正在测试的是什么,选项可能是什么?为什么要将each()
与div.div1
一起使用?只有一个这样的元素。@BackinaFlash-…即使有更多的元素,.hide()
将处理所有这些元素。嗨@回到Flash中,我使用each(),因为我需要从这个.config.option获取每个值,有些值可能是假的。谢谢3个人真的投票赞成吗?我已经把这个问题重读了6遍,但我仍然不知道他在这里试图实现什么……以及div
的可见性究竟取决于什么条件?你能解释一下你正在测试的是什么,选项可能是什么吗?这与问题无关,因为show()
/hide()
取决于对条件的评估。大概,OP知道什么是“if”语句?大概,他(从问题中)也清楚地知道如何使用hide()
。因此,如果没有更多的信息,所有人能做的,而且似乎正在做的,都是留下猜测。我没有投反对票,但这个答案似乎没有什么用处。这与问题无关,因为show()
/hide()
取决于对条件的评估。大概,OP知道什么是“if”语句?大概,他(从问题中)也清楚地知道如何使用hide()
。因此,如果没有更多的信息,所有人能做的,而且似乎正在做的,都是留下猜测。我没有投反对票,但这个答案似乎没有用。