![](/img/trans.png)
[英]scrollbars rendering on some divs but not others [jScrollPane / jQuery]
[英]Show some divs when others are hidden with jQuery
我希望當CATEGORY處於活動狀態時,要隱藏.closeorshow
div。 如果未選擇類別,則應顯示.closeorshow
div。
我用$(".closeorshow").hide()
和.show()
進行了一些測試,但我是jQuery的初學者,我無法弄清楚。 問題似乎是,每當我取消選擇任何類別而不是未選擇任何類別時, .closeorshow
div就會重新出現。
這是一個小提琴 。
HTML:
<ul class="types">
<li class="item" data-target="games">Games</li>
<li class="item" data-target="music">Music</li>
<li class="item" data-target="movies">Movies</li>
<li class="item" data-target="ljudobild">Ljud & Bild</li>
</ul>
<div class="description" id="games">Description of Games</div>
<div class="description" id="music">Description of Music</div>
<div class="description" id="movies">Description of Movies</div>
<div class="description" id="ljudobild">Description of Television</div>
<br><br>
<div class="closeorshow">
This class are going to be SHOWN when NONE of the items are selected, and HIDED
when one of the items is selected.
</div>
JS:
$(document).ready( function() {
$('.types li').click( function(t) {
$('.closeorshow').hide();
if ($(this).hasClass('active')) {
$(this).removeClass('active');$(this).addClass('item');
$('.closeorshow').show();
} else {
$(this).addClass('active');$(this).removeClass('item');
}
$('#'+$(this).attr('data-target')).toggle();
});
});
基本思想是檢查類.active
是否存在。 這是通過調用$(".active")
,它將生成一個數組,這意味着它可以訪問.length
方法。 如果數組的length
大於0,則不希望顯示.closeorshow
,因此請調用$(".closeorshow").hide()
。 否則,您確實希望顯示它,因此調用$("#.closeorshow").show()
。
告訴我一切是否都沒有。
我只更改了您的JavaScript:
$(document).ready(function () {
$('.types li').on("click", function () {
$(this).toggleClass("active item");
$('#' + $(this).attr("data-target")).toggle();
if ($(".active").length === 0)
$(".closeorshow").show();
else
$(".closeorshow").hide();
});
});
這是最初為演示相同概念而制作的小提琴 。 僅作參考之用,因為它比您的問題更簡單,但是仍然展示了如何解決有問題的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.