gpt4 book ai didi

javascript - Jquery 过滤器隐藏了 div 里面的 div

转载 作者:行者123 更新时间:2023-11-28 12:31:20 28 4
gpt4 key购买 nike

我有 2 个过滤器用于过滤一些 div。但是当按下过滤器按钮时,div 内容被隐藏。为了更好的例子,我构建了一个 JSFiddle

JSFiddle

HTML 是:

<!-- Rating  Filter Menu -->
<h4><a href="#rating-filter">Rating</a></h4>
<div id="rating-filter">
<div>
<ul>
<li data-stars-id="alls"> <a href="#">Toate<small class="total"></small></a> </li>
<li data-stars-id="3"> <a href="#">3 STARS<small class="total-3"></small></a> </li>
<li data-stars-id="4"> <a href="#">4 STARS<small class="total-4"></small></a> </li>
<li data-stars-id="5"> <a href="#">5 STARS<small class="total-1"></small></a></li>
</ul>
</div>
</div>
<!-- Board Filter Menu -->
<h4><a href="#board-filter">Board Type</a></h4>
<div id="board-filter">
<div>
<ul>
<li data-board-id="allb"> <a href="#">Toate<small class="total"></small></a> </li>
<li data-board-id="Half Board"> <a href="#">Half Board<small class="total-hb"></small></a> </li>
<li data-board-id="Full Board"> <a href="#">Full Board<small class="total-fb"></small></a> </li>
<li data-board-id="Breakfast"> <a href="#">Breakfast<small class="total-bb"></small></a></li>
</ul>
</div>
</div>
<!-- End Menu -->
<!-- Start Listing -->
<div id="hotel-list" class="hotel-list">
<div stars="3" board="Full Board"><div>Test for 3 Stars / Full Board</div>3 STARS / FULL BOARD</div>
<div stars="3" board="Breakfast"><div>Test for 3 Stars / Breakfast</div>3 STARS / BREAKFAST</div>
<div stars="4" board="Half Board"><div>Test for 4 Stars / Half Board</div>4 STARS / HALF BOARD</div>
<div stars="5" board="Full Board"><div>Test for 5 Stars / Full Board</div>5 STARS / FULL BOARD</div>
<div stars="4" board="Half Board"><div>Test for 4 Stars / Half Board</div>4 STARS / HALF BOARD</div>
<div stars="5" board="Full Board"><div>Test for 5 Stars / Full Board</div>5 STARS / FULL BOARD</div>
</div>
<!-- End Listing -->

jQuery 是:

//Rating
$("#rating-filter li").on("click", function () {
id = $(this).attr("data-stars-id");
if (id == "alls") {
$("div[stars]").show()
} else {
$('#hotel-list div').show();
$('#hotel-list div').filter(function(){
return $(this).attr('stars') != id
}).hide();
}
return false;
});
//Board
$("#board-filter li").on("click", function () {
id = $(this).attr("data-board-id");
if (id == "allb") {
$("div[board]").show()
} else {
$('#hotel-list div').show();
$('#hotel-list div').filter(function(){
return $(this).attr('board') != id
}).hide();
}
return false;
});

因此代码以某种方式进行了归档,但它隐藏了其中的其他 div 的内容。

最佳答案

我强烈建议您将这些自定义属性替换为 data 属性,并在选择 hotels/filters 元素时使用类。在 jQuery 中扩展和控制会容易得多,只需对两个(或任意多个)过滤器使用单个 on click 事件:

$(".filters li").on("click", function () {
id = $(this).data("id");
filter = $(this).data("filter");
$("#hotel-list .hotels").show() && id == "all" || $('#hotel-list .hotels:not([data-'+filter+'="'+id+'"])').hide();
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Rating Filter Menu -->
<h4><a href="#rating-filter">Rating</a></h4>
<div id="rating-filter" class="filters">
<div>
<ul>
<li data-id="all" data-filter="stars"> <a href="#">Toate<small class="total"></small></a> </li>
<li data-id="3" data-filter="stars"> <a href="#">3 STARS<small class="total-3"></small></a> </li>
<li data-id="4" data-filter="stars"> <a href="#">4 STARS<small class="total-4"></small></a> </li>
<li data-id="5" data-filter="stars"> <a href="#">5 STARS<small class="total-1"></small></a></li>
</ul>
</div>
</div>
<!-- Board Filter Menu -->
<h4><a href="#board-filter">Board Type</a></h4>
<div id="board-filter" class="filters">
<div>
<ul>
<li data-id="all" data-filter="board"> <a href="#">Toate<small class="total"></small></a> </li>
<li data-id="Half Board" data-filter="board"> <a href="#">Half Board<small class="total-hb"></small></a> </li>
<li data-id="Full Board" data-filter="board"> <a href="#">Full Board<small class="total-fb"></small></a> </li>
<li data-id="Breakfast" data-filter="board"> <a href="#">Breakfast<small class="total-bb"></small></a></li>
</ul>
</div>
</div>
<!-- End Menu -->
<!-- Start Listing -->
<div id="hotel-list" class="hotel-list">
<div data-stars="3" data-board="Full Board" class="hotels"><div>Test for 3 Stars / Full Board</div>3 STARS / FULL BOARD</div>
<div data-stars="3" data-board="Breakfast" class="hotels"><div>Test for 3 Stars / Breakfast</div>3 STARS / BREAKFAST</div>
<div data-stars="4" data-board="Half Board" class="hotels"><div>Test for 4 Stars / Half Board</div>4 STARS / HALF BOARD</div>
<div data-stars="5" data-board="Full Board" class="hotels"><div>Test for 5 Stars / Full Board</div>5 STARS / FULL BOARD</div>
<div data-stars="4" data-board="Half Board" class="hotels"><div>Test for 4 Stars / Half Board</div>4 STARS / HALF BOARD</div>
<div data-stars="5" data-board="Full Board" class="hotels"><div>Test for 5 Stars / Full Board</div>5 STARS / FULL BOARD</div>
</div>
<!-- End Listing -->

关于javascript - Jquery 过滤器隐藏了 div 里面的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28611989/

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