2 回答
TA贡献1785条经验 获得超8个赞
如果您对 HTML 有一定的灵活性,我的建议是:
我认为如果您不需要 a 标签,只要将 aclass="filter_to" data-filter_to_term="element 1"标签移至 p 标签即可。
const select_all = (selector, selectee = document) =>
Array.from(selectee.querySelectorAll(selector));
const hide_item = item => item.style.display = 'none';
const show_item = item => item.style.display = '';
const item_text = item => item.textContent.toLowerCase();
const compare = text => item => item_text(item).includes(text);
const has_class = class_name => item => item.className.includes(class_name);
const not_has_class = class_name => item => !has_class(class_name)(item);
const filter_list = text => list => {
let lis = select_all("li", list);
let to_show = lis
.filter(not_has_class('none'))
.filter(compare(text));
if (to_show.length === 0)
to_show = lis.filter(has_class('none'));
lis.forEach(hide_item);
to_show.forEach(show_item);
};
const search_box = document.getElementById('searchCombo');
const filter = (text) =>
select_all(".combo").forEach(
filter_list((text || '').toLowerCase())
);
const set_filter = (text) => {
search_box.value = text;
filter(text);
};
const onclick_filter_to = (event) => {
event.preventDefault();
set_filter(event.target.dataset.filter_to_term);
};
select_all(".filter_to").forEach(
(element) => element.addEventListener("click", onclick_filter_to)
);
search_box.addEventListener("input", event => filter(event.target.value));
filter();
<p><a href="#" class="filter_to" data-filter_to_term="element 1">Filter for: vitamins</a></p>
<p><a href="#" class="filter_to" data-filter_to_term="element 2">Filter for: drugs</a></p>
<p><a href="#" class="filter_to" data-filter_to_term="element 3">Filter for: medications</a></p>
<input type="search" id="searchCombo" placeholder="Search for combination...">
<ul ID="list1" class="combo">
<li><a href="#">List 1 element 1</a></li>
<li><a href="#">List 1 element 2</a></li>
<li><a href="#">List 1 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
<ul ID="list2" class="combo">
<li><a href="#">List 2 element 1</a></li>
<li><a href="#">List 2 element 2</a></li>
<li><a href="#">List 2 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
<ul ID="list3" class="combo">
<li><a href="#">List 3 element 1</a></li>
<li><a href="#">List 3 element 2</a></li>
<li><a href="#">List 3 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
看起来用 input 替换 keyup 可能适用于 Chrome 中的取消 X 按钮(但我以前没有使用过)。
TA贡献1773条经验 获得超3个赞
这是工作演示Jsbin 工作演示
超文本标记语言
<p id="filter1">Filter for: vitamins</p>
<p id="filter2">Filter for: drugs</p>
<p id="filter3">Filter for: medications</p>
<input type="search" id="searchCombo" placeholder="Search for combination...">
<ul ID="list1" class="combo">
<li><a href="#">List 1 element 1</a></li>
<li><a href="#">List 1 element 2</a></li>
<li><a href="#">List 1 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
<ul ID="list2" class="combo">
<li><a href="#">List 2 element 1</a></li>
<li><a href="#">List 2 element 2</a></li>
<li><a href="#">List 2 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
<ul ID="list3" class="combo">
<li><a href="#">List 3 element 1</a></li>
<li><a href="#">List 3 element 2</a></li>
<li><a href="#">List 3 element 3</a></li>
<li class="none">Nothing found in this category</li>
</ul>
JS
const select_all = (selector, selectee = document) =>
Array.from(selectee.querySelectorAll(selector));
const hide_item = item => item.style.display = 'none';
const show_item = item => item.style.display = '';
const item_text = item => item.textContent.toLowerCase();
const compare = text => item => item_text(item).includes(text);
const has_class = class_name => item => item.className.includes(class_name);
const not_has_class = class_name => item => !has_class(class_name)(item);
const filter_list = text => list => {
let lis = select_all("li", list);
let to_show = lis
.filter(not_has_class('none'))
.filter(compare(text));
if (to_show.length === 0)
to_show = lis.filter(has_class('none'));
lis.forEach(hide_item);
to_show.forEach(show_item);
};
const filter = (value) =>
select_all(".combo").forEach(
filter_list((value || '').toLowerCase())
);
const searchCombo = document.getElementById('searchCombo')
searchCombo.addEventListener("keyup", function(e) {
filter(e.target.value)
});
//Trigger filter when search input field cancel is clicked
searchCombo.addEventListener("search", function(e) {
filter('')
});
function trigger(value) {
searchCombo.value = value;
filter(value)
}
document.getElementById('filter1').addEventListener("click", function() {
trigger('element 1')
});
document.getElementById('filter2').addEventListener("click", function() {
trigger('element 2')
});
document.getElementById('filter3').addEventListener("click", function() {
trigger('element 3')
});
filter();
添加回答
举报