Javascript 按配料过滤比萨饼
我有按钮,你可以选择过滤哪些成分Javascript 按配料过滤比萨饼,javascript,html,for-loop,filter,htmlcollection,Javascript,Html,For Loop,Filter,Htmlcollection,我有按钮,你可以选择过滤哪些成分 //index.html <div class="submenu"> <input type="checkbox" class="filter-item" id="olive" onclick="filter()" /> <label for="olive"> <img alt="olive" src="img/png/pizza/olive.png" /> </la
//index.html
<div class="submenu">
<input type="checkbox" class="filter-item" id="olive" onclick="filter()" />
<label for="olive">
<img alt="olive" src="img/png/pizza/olive.png" />
</label>
<input type="checkbox" class="filter-item" id="shrimp" onclick="filter()" />
<label for="shrimp">
<img alt="shrimp" src="img/png/pizza/shrimp.png" />
</label>
<input type="checkbox" class="filter-item" id="mushroom" onclick="filter()" />
<label for="mushroom">
<img alt="mushroom" src="img/png/pizza/champion.png" />
</label>
<input type="checkbox" class="filter-item" id="pepperoni" onclick="filter()" />
<label for="pepperoni">
<img alt="pepperoni" src="img/png/pizza/pepperoni.png" />
</label>
<footer id="filter-text">no filter applied</footer>
</div>
这在某种程度上是可行的。它只对第一个成分“olive”有效,它正确地添加了类显示,但是当复选框未选中时,它不会删除类。其他成分甚至不会添加到数组itemsToFilter中
有人有办法吗?。。。我不小心把显示比萨饼的for循环放在for循环中,for循环通过配料按钮。我将其移出一步,并将从pizzas中移除show类的部分移到循环数组itemsToFilter的for循环之前,因为如果数组为空,它不会移除show类。谢谢大家。我重新编写了您的代码并做了一些更改:
var itemsToFilter = [];
//HTMLCollections, not arrays
ingredients = document.getElementsByClassName("filter-item");
pizzas = document.getElementsByClassName("menu-item");
function filter() {
// Here you have always only the selected ingredients
let selectedIngredients = ingredients.filter(ingredient => ingredient.checked === true);
if(selectedIngredients.length === 0) {
pizzas.forEach(pizza => {
pizza.classList.add("show");
});
} else {
pizzas.forEach(pizza => {
let hasAllIngredients = true;
selectedIngredients.forEach(ingredient => {
if(!pizza.classList.includes(ingredient.getAttribute("id"))) {
hasAllIngredients = false;
}
});
if(hasAllIngredients) {
pizza.classList.add("show");
} else {
pizza.classList.remove("show");
}
});
}
}
var itemsToFilter = [];
//HTMLCollections, not arrays
ingredients = document.getElementsByClassName("filter-item");
pizzas = document.getElementsByClassName("menu-item");
function filter() {
for (var i = 0; i < ingredients.length; i++) {
if (ingredients[i].checked == true) {
//add checked item to array itemsToFilter
if (itemsToFilter.includes(ingredients[i].getAttribute("id")) == false) {
itemsToFilter.push(ingredients[i].getAttribute("id"));
}
} else {
//remove unchecked item from array itemsToFilter
for (var y = itemsToFilter.length - 1; y >= 0; y--) {
if (itemsToFilter[y] === ingredients[i].getAttribute("id")) {
if (itemsToFilter.includes(ingredients[i].getAttribute("id")) == true) {
itemsToFilter.splice(y, 1);
}
}
}
}
//show pizzas with classes that match items in array itemsToFilter
for (var i = 0; i < pizzas.length; i++) {
for (y in itemsToFilter) {
if (pizzas[i].classList.contains(itemsToFilter[y]) == false) {
if (pizzas[i].classList.contains("show") == true) {
pizzas[i].classList.remove("show");
}
break;
} else {
pizzas[i].classList.add("show");
}
}
}
}
}
var itemsToFilter = [];
//HTMLCollections, not arrays
ingredients = document.getElementsByClassName("filter-item");
pizzas = document.getElementsByClassName("menu-item");
function filter() {
// Here you have always only the selected ingredients
let selectedIngredients = ingredients.filter(ingredient => ingredient.checked === true);
if(selectedIngredients.length === 0) {
pizzas.forEach(pizza => {
pizza.classList.add("show");
});
} else {
pizzas.forEach(pizza => {
let hasAllIngredients = true;
selectedIngredients.forEach(ingredient => {
if(!pizza.classList.includes(ingredient.getAttribute("id"))) {
hasAllIngredients = false;
}
});
if(hasAllIngredients) {
pizza.classList.add("show");
} else {
pizza.classList.remove("show");
}
});
}
}