我有此代码,但无法正常工作。
let productBox = document.querySelectorAll('.product');
let selectCategory = document.querySelector('#select-category');
let categoryVal = document.querySelectorAll('#select-category option');
selectCategory.addEventListener('change', () => {
Array.prototype.forEach.call(categoryVal, catItem => {
Array.prototype.forEach.call(productBox, prodItem => {
if((catItem.selected) && (prodItem.category ===
catItem.category)) {
prodItem.style.display = 'none'
}
})
})
})
.product {
border: 1px solid;
padding: 2px;
margin: .5rem;
}
<select id="select-category">
<option category="all">All</option>
<option category="dinner">Dinner</option>
<option category="first meal">First meal</option>
<option category="garnish">Garnish</option>
</select>
<div category="dinner" class="product">Dinner</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish</div>
我需要在选择option
元素时,将display none
应用于类别不匹配的块。最好的方法是什么?
答案 0 :(得分:1)
您的代码中存在许多问题,并且您的HTML无效。
HTML5中没有定义category
属性。如果要使用自定义属性,则应在它们前面加上data-*
。对于select
选项,您应该只使用value
属性。
修复此问题后,逻辑只需:
select
元素发生更改,则从中获取所选值。div
元素以及每个元素:
all
,或者与元素的data-category
相匹配,请将显示样式设置为其默认样式(''
)none
。这是完整的代码段:
const productDivs = document.querySelectorAll('.product');
const categorySelect = document.querySelector('#select-category');
categorySelect.addEventListener('change', (e) => {
const category = e.target.value;
[...productDivs].forEach(pd => {
const display = pd.dataset.category === category || category === 'all';
pd.style.display = display ? '' : 'none';
});
});
<select id="select-category">
<option value="all">All</option>
<option value="dinner">Dinner</option>
<option value="first meal">First meal</option>
<option value="garnish">Garnish</option>
</select>
<div data-category="dinner" class="product">Dinner</div>
<div data-category="first meal" class="product">First meal</div>
<div data-category="garnish" class="product">Garnish</div>
答案 1 :(得分:0)
工作正常。让我知道是您的期望
<!DOCTYPE html>
<html>
<head>
<title>Week One</title>
<link href="./style_home.css" type="text/css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Baloo+Chettan|Montserrat|Oswald|ZCOOL+XiaoWei" rel="stylesheet">
</head>
<body>
<div class="topbar">
<table class="topbar">
<tr>
<td><a class="topbar">Home</a></td>
<td><a class="topbar">About</a></td>
<td><a class="topbar">Contact</a></td>
</tr>
</table>
</div>
</body>
</html>
答案 2 :(得分:0)
function selectCategory(element) {
const value = element.value;
const products = document.getElementsByClassName('product');
Array.prototype.forEach.call(products, product => {
const productAttr = product.getAttribute('category');
if(value === 'All'){
product.style.display = 'block';
return;
}
productAttr !== value.toLowerCase() ?
product.style.display = 'none' :
product.style.display = 'block';
})
}
<select id="select-category" onChange="selectCategory(this)">
<option category="all">All</option>
<option category="dinner">Dinner</option>
<option category="first meal">First meal</option>
<option category="garnish">Garnish</option>
</select>
<div category="dinner" class="product">Dinner</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish</div>
答案 3 :(得分:0)
category
重命名为value
{}
包裹了整个代码,所以我可以使用局部变量input
事件代替了change
事件hide
而不是直接设置样式product
的类设置为hide
(不适用于选项all
),然后为具有适当类别值的元素取消该类的设置。
console.clear();
{
document.addEventListener('input', (e) => {
const selectCategory = document.querySelector('#select-category');
if (e.target === selectCategory) {
const productBoxes = document.querySelectorAll('.product');
const categoryVal = document.querySelectorAll('#select-category option');
if (selectCategory.value == 'all') {
show(productBoxes)
} else {
hide(productBoxes)
}
const matches = document.querySelectorAll(`[category*="${selectCategory.value}"]`)
show(matches)
}
})
const hide = (items) => {
Array.prototype.forEach.call(items, prodItem => {
prodItem.classList.add('hide')
})
}
const show = (items) => {
Array.prototype.forEach.call(items, prodItem => {
prodItem.classList.remove('hide')
})
}
}
.product {
border: 1px solid;
padding: 2px;
margin: .5rem;
}
.hide {
display: none;
}
<select id="select-category">
<option value="all">All</option>
<option value="dinner">Dinner</option>
<option value="first meal">First meal</option>
<option value="garnish">Garnish</option>
</select>
<div category="dinner" class="product">Dinner: One</div>
<div category="first meal" class="product">First meal</div>
<div category="garnish" class="product">Garnish: one</div>
<div category="garnish" class="product">Garnish: two</div>
<div category="dinner" class="product">Dinner: Two</div>
<div category="dinner" class="product">Dinner: three</div>
<br><br><select><option>totally<option>unrelated<option>select</select>