所以我尝试过这种方式,可以通过多个条件过滤这些框,例如当我选择“ ON-TRACK”然后选择“ International”时,它应该向我显示唯一带有“ International”标题的ON-TRACK框...如果我选择“ ON- TRACK&OFF-Track and Technology它应该什么也不显示,依此类推..我厌倦了if语句的使用,但是我找不到方法。也有循环和相同..任何提示?谢谢
$(document).ready(function() {
let toShow = [];
let noEl = [];
$('.top-right a').click(function(e) {
$(e.target).toggleClass('tag-active');
let getId = e.target.id;
let tagName = e.target.innerHTML;
if (toShow.includes(getId)) {
for (var i = 0; i < toShow.length; i ++) {
if (toShow[i] == getId) {
toShow.splice(i, 1);
break;
}
}
}
else {
toShow.push(getId);
}
if (noEl.includes(getId)) {
for (let j = 0; j < noEl.length; j ++) {
if (noEl[j] == getId) {
$(`.list-body .${getId}`).fadeOut('fast');
noEl.splice(j, 1);
break;
}
}
}
if (toShow.length === 0) {
$('#main .list-el').fadeIn('fast');
$(`.list-body .tag-no-result`).fadeOut('fast');
}
else {
$('#main .list-el').fadeOut('fast');
}
$.each(toShow, function(i, b) {
$('#main .list-el'+'.'+b).fadeIn('fast');
});
});
});
.tag {
border: 1px solid blue;
display: inline-block;
padding: 10px;
}
.myull {
display: inline-block;
border: 1px solid blue;
padding: 5px
}
.btn {
padding: 0px;
font-size: 14px;
}
a {
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body class="m-4">
<div class="list">
<div class="top">
<div class="top-right">
<a id="tag-1" class="myull tag tag-1" href='#' class='tag'>ON-TRACK</a>
<a id="tag-2" class="myull tag tag-2" href='#' class='tag'>OFF-TRACK</a>
<a id="tag-3" class="myull tag tag-3" href='#' class='tag'>CRITICAL</a>
<a id="tag-4" class="myull tag tag-4" href='#' class='tag'>NOT-STARTED</a>
<a id="tag-5" class="myull tag tag-5" href='#' class='tag'>ONHOLD</a>
<a id="tag-6" class="myull tag tag-6" href='#' class='tag'>CLOSED</a>
</div>
</div>
<hr>
<p class="pl-2">PORTFOLIO:</p>
<div class="top">
<div class="top-right myul">
<a id="tag-7" class="myull tag-7" href='#'>Support</a>
<a id="tag-8" class="myull tag-8" href='#'>Collaboration</a>
<a id="tag-9" class="myull tag-9" href='#'>Quality</a>
<a id="tag-10" class="myull tag-10" href='#'>DO</a>
<a id="tag-11" class="myull tag-11" href='#'>Social</a>
<a id="tag-12" class="myull tag-12" href='#'>University</a>
<a id="tag-13" class="myull tag-13" href='#'>Technology</a>
</div>
</div>
<hr>
<p class="pl-2">OWNER:</p>
<div class="top">
<div class="top-right myul1">
<a id="tag-14" class="myull tag-14" href='#'>Jake</a>
<a id="tag-15" class="myull tag-15" href='#'>Adam</a>
<a id="tag-16" class="myull tag-16" href='#'>Mark </a>
<a id="tag-17" class="myull tag-17" href='#'>Sam </a>
<a id="tag-18" class="myull tag-18" href='#'>Sarah </a>
</div>
</div>
</div>
<div class="container" style="background-color: aqua">
<div class="row">
<div id="main" class="col-md-12 parentClass">
<div class="tag list-el tag-14 tag-1 tag-7 m-1">
<div>
<h4>ON-TRACK</h4>
</div>
<div>
<h5>Support</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-14 tag-1 tag-8 m-1">
<div>
<h4>ON-TRACK</h4>
</div>
<div>
<h5>Collaboration</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-14 tag-2 tag-9 m-1">
<div>
<h4>OFF-TRACK</h4>
</div>
<div>
<h5>Quality</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-15 tag-2 tag-10 m-1">
<div>
<h4>OFF-TRACK</h4>
</div>
<div>
<h5>DO</h5>
</div>
<div>
<h6>Adam</h6>
</div>
</div>
<div class="tag list-el tag-16 tag-3 tag-11 m-1">
<div>
<h4>CRITICAL</h4>
</div>
<div>
<h5>Social</h5>
</div>
<div>
<h6>Mark</h6>
</div>
</div>
<div class="tag list-el tag-17 tag-4 tag-12 m-1">
<div>
<h4>NOT-STARTED</h4>
</div>
<div>
<h5>University</h5>
</div>
<div>
<h6>Sam</h6>
</div>
</div>
<div class="tag list-el tag-18 tag-4 tag-13 m-1">
<div>
<h4>ONHOLD</h4>
</div>
<div>
<h5>Technology</h5>
</div>
<div>
<h6>Sarah</h6>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
看,如果在特定的切换处于活动状态时添加类名,然后通过“活动”类获取当前选择的过滤器,然后使用处于活动状态的切换键。
我已经完全重写了JavaScript事件处理程序。请参阅下面的代码中的注释,以了解每一行的作用:
$(function() {
// Filter in the tag list clicked
$('.list').on('click', '.tag', function() {
// Toggle active class on clicked element
$(this).toggleClass('active');
// Get all active filters' tag class number (tag-xx)
let activeGp1 = $('.top:eq(0) .tag.active').map((i, el) => el.className.match(/tag-\d+/)[0]).get();
let activeGp2 = $('.top:eq(1) .tag.active').map((i, el) => el.className.match(/tag-\d+/)[0]).get();
let activeGp3 = $('.top:eq(2) .tag.active').map((i, el) => el.className.match(/tag-\d+/)[0]).get();
// Filter the items below, must match at least one single active filter in each filter group
$('#main .list-el').hide().filter((i, el) =>
(activeGp1.length == 0 || activeGp1.some(c => el.classList.contains(c))) &&
(activeGp2.length == 0 || activeGp2.some(c => el.classList.contains(c))) &&
(activeGp3.length == 0 || activeGp3.some(c => el.classList.contains(c)))
).show();
// Return false since it's not a hyperlink
return false;
});
});
.tag {
border: 1px solid blue;
display: inline-block;
padding: 10px;
}
.tag.active {
background-color: #ffe;
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2);
}
.myull {
display: inline-block;
border: 1px solid blue;
padding: 5px
}
.btn {
padding: 0px;
font-size: 14px;
}
a {
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body class="m-4">
<div class="list">
<div class="top">
<div class="top-right">
<a class="myull tag tag-1">ON-TRACK</a>
<a class="myull tag tag-2">OFF-TRACK</a>
<a class="myull tag tag-3">CRITICAL</a>
<a class="myull tag tag-4">NOT-STARTED</a>
<a class="myull tag tag-5">ONHOLD</a>
<a class="myull tag tag-6">CLOSED</a>
</div>
</div>
<hr>
<p class="pl-2">PORTFOLIO:</p>
<div class="top">
<div class="top-right myul">
<a class="myull tag tag-7">Support</a>
<a class="myull tag tag-8">Collaboration</a>
<a class="myull tag tag-9">Quality</a>
<a class="myull tag tag-10">DO</a>
<a class="myull tag tag-11">Social</a>
<a class="myull tag tag-12">University</a>
<a class="myull tag tag-13">Technology</a>
</div>
</div>
<hr>
<p class="pl-2">OWNER:</p>
<div class="top">
<div class="top-right myul1">
<a class="myull tag tag-14">Jake</a>
<a class="myull tag tag-15">Adam</a>
<a class="myull tag tag-16">Mark </a>
<a class="myull tag tag-17">Sam </a>
<a class="myull tag tag-18">Sarah </a>
</div>
</div>
</div>
<div class="container" style="background-color: aqua">
<div class="row">
<div id="main" class="col-md-12 parentClass">
<div class="tag list-el tag-14 tag-1 tag-7 m-1">
<div>
<h4>ON-TRACK</h4>
</div>
<div>
<h5>Support</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-14 tag-1 tag-8 m-1">
<div>
<h4>ON-TRACK</h4>
</div>
<div>
<h5>Collaboration</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-14 tag-2 tag-9 m-1">
<div>
<h4>OFF-TRACK</h4>
</div>
<div>
<h5>Quality</h5>
</div>
<div>
<h6>Jake</h6>
</div>
</div>
<div class="tag list-el tag-15 tag-2 tag-10 m-1">
<div>
<h4>OFF-TRACK</h4>
</div>
<div>
<h5>DO</h5>
</div>
<div>
<h6>Adam</h6>
</div>
</div>
<div class="tag list-el tag-16 tag-3 tag-11 m-1">
<div>
<h4>CRITICAL</h4>
</div>
<div>
<h5>Social</h5>
</div>
<div>
<h6>Mark</h6>
</div>
</div>
<div class="tag list-el tag-17 tag-4 tag-12 m-1">
<div>
<h4>NOT-STARTED</h4>
</div>
<div>
<h5>University</h5>
</div>
<div>
<h6>Sam</h6>
</div>
</div>
<div class="tag list-el tag-18 tag-4 tag-13 m-1">
<div>
<h4>ONHOLD</h4>
</div>
<div>
<h5>Technology</h5>
</div>
<div>
<h6>Sarah</h6>
</div>
</div>
</div>
</div>
</div>