Javascript jQuery如果一个元素可见,请关闭另一个元素
我想知道是否有一种方法可以在另一个元素可见后隐藏该元素。这是我现在的jQuery代码Javascript jQuery如果一个元素可见,请关闭另一个元素,javascript,jquery,Javascript,Jquery,我想知道是否有一种方法可以在另一个元素可见后隐藏该元素。这是我现在的jQuery代码 var fade = $("#listofeffects ul:nth-of-type(2)"); var firstchild = $("#listofeffects ul:first-child"); firstchild.click(function(){ $(".element1").fadeToggle(); }); fade.click(function(){ $
var fade = $("#listofeffects ul:nth-of-type(2)");
var firstchild = $("#listofeffects ul:first-child");
firstchild.click(function(){
$(".element1").fadeToggle();
});
fade.click(function(){
$(".element2").fadeToggle();
});
当.element1可见时,.element2无法显示。我研究过使用if/else javascript语句,但没有发现任何有效的方法。我还研究了使用:visible和:hidden,这两种方法都不起作用
谢谢。试试这个。
首先在单击时隐藏所有元素,然后使用jQuery显示单击的元素(此)
例如:
试试这个。
首先在单击时隐藏所有元素,然后使用jQuery显示单击的元素(此)
例如:
如果你的代码是这样的
<div class="div1 divClass"> Div 1 text </div>
<div class="div2 divClass"> Div 2 text </div>
<div class="div3 divClass"> Div 3 text </div>
<div class="div4 divClass"> Div 4 text </div>
此jquery代码将显示单击的元素和其他元素将自动隐藏…如果您的代码如下所示
<div class="div1 divClass"> Div 1 text </div>
<div class="div2 divClass"> Div 2 text </div>
<div class="div3 divClass"> Div 3 text </div>
<div class="div4 divClass"> Div 4 text </div>
此jquery代码将显示单击的元素和其他元素将自动隐藏…假设您的HTML有不同的
事件
附加到两个不同的
var firstchild = $("#listofeffects ul:first-child");
var fade = $("#listofeffects ul:nth-of-type(2)");
$('.element2').hide(); // initially show only .element1
$("#listofeffects").on('click','ul',function(e){
$('.element1,.element2').hide();
( $(e.currentTarget).is(firstchild) ) ? $('.element1').fadeIn('slow') : $('.element2').fadeIn('slow') ;
});
假设您的HTML有不同的
事件
附加到两个不同的
var firstchild = $("#listofeffects ul:first-child");
var fade = $("#listofeffects ul:nth-of-type(2)");
$('.element2').hide(); // initially show only .element1
$("#listofeffects").on('click','ul',function(e){
$('.element1,.element2').hide();
( $(e.currentTarget).is(firstchild) ) ? $('.element1').fadeIn('slow') : $('.element2').fadeIn('slow') ;
});
你能在问题中包括
html
吗?你能在问题中包括html
吗?