有人知道如何为选项而不是按钮做这个吗?脚本应该过滤HTML部分。我的问题是,显然它不适用于<option>
而不是<button>
。结果是不显示任何东西,而不是显示all类。
CSS
.show {
display: block;
}
.filterDiv {
display: none;
}
这是过滤器部分。默认情况下,它应该在所有
上设置。<div class="col-lg-6 col-sm-6 col-12" id="myBtnContainer">
<div class="release-filter justify-content-end d-flex">
<div class="filter-title">
<select name="year">
<option class="btn active" value="" onclick="filterSelection('all')" disabled selected>Filter By Year</option>
<option class="btn" value="2023" onclick="filterSelection('2023')">2023</option>
<option class="btn" value="2022" onclick="filterSelection('2022')">2022</option>
<option class="btn" value="2021" onclick="filterSelection('2021')">2021</option>
<option class="btn" value="2020" onclick="filterSelection('2020')">2020</option>
</select>
</div>
</div>
</div>
这是应该过滤的项之一。但如上所述,它不起作用。它根本不过滤任何内容,最终也不显示任何内容。
<div class="col-xl-6 col-lg-6 col-12 filterDiv 2023">
<div class="collection-release">
</div>
</div>
JS
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
</script>
我编辑了代码,以便无论用户选择哪一年,该年份的div将在屏幕上可见,而其他年份将被隐藏。我希望这对你有帮助。
var mySelect = document.getElementById('yearSelect');
mySelect.onchange = (event) => { //catch select option changes.
var inputText = event.target.value; // get selected option value.
var x, i;
x = document.getElementsByClassName("filterDiv");
if (inputText == "all") inputText = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].classList.contains(inputText)) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
element.classList.add(name);
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
element.classList.remove(name);
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
.show {
display: block !important;
}
.filterDiv {
display: none;
}
<div class="col-lg-6 col-sm-6 col-12" id="myBtnContainer">
<div class="release-filter justify-content-end d-flex">
<div class="filter-title">
<select name="year" id="yearSelect">
<option class="btn active" value="" onclick="filterSelection('all')" disabled selected>Filter By Year</option>
<option class="btn" value="2023">2023</option>
<option class="btn" value="2022">2022</option>
<option class="btn" value="2021">2021</option>
<option class="btn" value="2020">2020</option>
</select>
</div>
</div>
</div>
<div class="col-xl-6 col-lg-6 col-12 show filterDiv 2023">
<div class="collection-release">
2023 filter div
</div>
</div>
<div class="col-xl-6 col-lg-6 col-12 show filterDiv 2022">
<div class="collection-release">
2022 filter div
</div>
</div>
<div class="col-xl-6 col-lg-6 col-12 show filterDiv 2021">
<div class="collection-release">
2021 filter div
</div>
</div>
<div class="col-xl-6 col-lg-6 col-12 show filterDiv 2020">
<div class="collection-release">
2020 filter div
</div>
</div>