[英]JQUERY- show div on li click else hide
I have found the fiddle below and what I am trying to do is to make '#overlay' div to hide when I click somewhere else than on '.album' li. 我在下面找到了小提琴,我想做的是使“ #overlay” div在我单击“ .album” li以外的其他位置时隐藏。 Li '.album' should show '#overlay' div when clicked but when I click on '.album2' it should dissapear.
Li'.album'在单击时应显示'#overlay'div,但当我单击'.album2'时应消失。
JAVASCRIPT JAVASCRIPT
$(".album").click(function() {
$("#overlay").css("visibility", "visible");
CSS CSS
#overlay { visibility: hidden; }
HTML HTML
<ul>
<li class="album" id="nirvana-nevermind">
hello
<div id="overlay">
<a href="http://www.nirvana.com">Nirvana</a> Nevermind
</div>
</li>
<li class="album2" id="eminem">
hello
</li>
You could add this: 您可以添加以下内容:
$('#eminem').click(function(){
$("#nirvana-nevermind").css("display","none");
}
If you want to hide #overlay
when .album2
is clicked, use 如果你想隐藏
#overlay
时.album2
被点击时,使用
$(".album2").click(function() {
$("#overlay").css("visibility", "hidden");
});
Check the Updated Fiddle Here 在这里检查更新的小提琴
Just use display
none
instead of visibility
hidden
. 只需使用
none
display
而不是hidden
visibility
。 Although which method you are using will work definitely, but just will make the div invisible and it will keep occupying it's space. 尽管您使用的是哪种方法肯定可以工作,但是只会使div不可见,并且会不断占用其空间。
$(".album").click(function() { $("#overlay").css("display", "none"); //or you can use hide() method like below: //$("#overlay").hide(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="album" id="nirvana-nevermind"> hello <div id="overlay"> <a href="http://www.nirvana.com">Nirvana</a> Nevermind </div> </li> <li class="album2" id="eminem"> hello </li>
I'll suggest you this method: 我会建议您这种方法:
$('ul>li').on('click', function(){
$('ul>li').not(this).find('#overlay').css('visibility','hidden');
if ($(this).find('#overlay').length) {
$(this).find('#overlay').css('visibility','visible');
}
});
It waits to click on li
element then hidding
all #overlay
(not clicked one) and checks if this li
contains overlay inside of it, then set visibility
to visible
(nothing changes if already visible), this will work with more li
elements and more overlays within them. 它等待单击
li
元素,然后hidding
所有#overlay
(未单击),并检查this li
是否在其中包含叠加,然后将visibility
设置为visible
(如果已经可见,则什么都没有改变),这将与更多li
元素和更多元素一起工作它们中的叠加层。
$('ul>li').on('click', function(){ $('ul>li').not(this).find('#overlay').css('visibility','hidden'); if ($(this).find('#overlay').length) { $(this).find('#overlay').css('visibility','visible'); } });
#overlay { visibility: hidden; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="album" id="nirvana-nevermind"> hello <div id="overlay"> <a href="http://www.nirvana.com">Nirvana</a> Nevermind </div> </li> <li class="album2" id="eminem">hello</li> <li class="album2" id="">hello2</li> <li class="album2" id="">hello3</li> <li class="album2" id=""> hello with overlay <div id="overlay"> New overlay here </div> </li> </ul>
And also, you should know that visibility
will always be there, even if its setted to hidden
.. The alternative is to use display
none|block
and it will not expand the content if it's hidden. 而且,您应该知道
visibility
将始终存在,即使将其设置为hidden
..替代方法是使用display
none|block
并且如果隐藏了内容,则不会扩展内容。
$(document).ready(function(){ $(".album").click(function() { if($("#overlay").hasClass("hiden")) $("#overlay").removeClass("hiden").addClass("show"); }); $(".album2").click(function() { if($("#overlay").hasClass("show")) $("#overlay").removeClass("show").addClass("hiden"); }); });
.show { visibility: visible; } .hiden { visibility: hidden; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="album" id="nirvana-nevermind"> hello <div id="overlay" class="hiden"> <a href="http://www.nirvana.com">Nirvana</a> Nevermind </div> </li> <li class="album2" id="eminem"> hello </li> </ul>
您可以使用:not选择器在单击元素中添加例外,您可以在此找到更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.