gpt4 book ai didi

javascript - 如何重置过滤列表搜索框?

转载 作者:行者123 更新时间:2023-12-02 23:34:42 28 4
gpt4 key购买 nike

我创建了一个搜索框来过滤this W3Schools guideline之后的列表:

function myFunction() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}
#myUL li a:hover:not(.header) {
background-color: #eee;
}
<h2>My Phonebook</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
<li><a href="#">Billy</a></li>
<li><a href="#">Bob</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>
</ul>

我现在需要一个按钮,只需单击一下即可:
- 重置搜索框
- 再次显示完整列表

我在 stackoverflow 和互联网上的其他地方进行了搜索,但没有找到解决方案。

如下面的代码所示,我添加了一个链接到应该完成这项工作的 JavaScript 函数的重置按钮。然而,该按钮只会清除搜索框,尽管搜索框显示为空,但我需要单击搜索框并按“删除”一次才能恢复完整列表:

// CREATE A FILTER/SEARCH LIST
function myFunction() {
// Declare variables
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName('li');

// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}

// CLEAR SEARCH BOX
function clearInputFields(divElement) {
var ele = document.getElementById(divElement);

// IT WILL READ ALL THE ELEMENTS. <p>, <div>, <input> ETC.
for (i = 0; i < ele.childNodes.length; i++) {

// SINCE THE <input> FIELDS ARE INSIDE A <p> TAG,
// I'LL USE THE "firstChild" PROPERTY TO GET THE <input> TAG.
var child = ele.childNodes[i].firstChild;
//console.log(child);

// CHECK IF CHILD NOT NULL.
// THIS IS IMPORTANT AS IT WILL RETURN A TEXT FOR EVERY "Whitespace".
// 'Whitespace' IS A TEXT OR NODE BETWEEN <div> AND <p> AND AFTER <p>.
if (child) {
switch (child.type) {
case 'text':
child.value = '';
}
}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
<h2>My Phonebook</h2>

<div id="searchbox">
<p><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<p/>
</div>

<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
<li><a href="#">Billy</a></li>
<li><a href="#">Bob</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>
</ul>

<div id="title">
<a href="#216" onclick="clearInputFields('searchbox');">RESET</a>
</div>

有人可以告诉我我的代码有什么问题或建议更好的代码吗?

最佳答案

您可以使用 dispatchEvent() 触发 keyup 事件来通知更改已完成。

child.dispatchEvent(new Event('keyup'));

参见Creating and triggering events

function myFunction() {
// Declare variables
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName('li');

// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}

// CLEAR SEARCH BOX
function clearInputFields(divElement) {
var ele = document.getElementById(divElement);

// IT WILL READ ALL THE ELEMENTS. <p>, <div>, <input> ETC.
for (i = 0; i < ele.childNodes.length; i++) {

// SINCE THE <input> FIELDS ARE INSIDE A <p> TAG,
// I'LL USE THE "firstChild" PROPERTY TO GET THE <input> TAG.
var child = ele.childNodes[i].firstChild;
//console.log(child);

// CHECK IF CHILD NOT NULL.
// THIS IS IMPORTANT AS IT WILL RETURN A TEXT FOR EVERY "Whitespace".
// 'Whitespace' IS A TEXT OR NODE BETWEEN <div> AND <p> AND AFTER <p>.
if (child) {
switch (child.type) {
case 'text':
child.value = '';
child.dispatchEvent(new Event('keyup'));
}
}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
/* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
<h2>My Phonebook</h2>

<div id="searchbox">
<p><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<p/>
</div>

<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
<li><a href="#">Billy</a></li>
<li><a href="#">Bob</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>
</ul>

<div id="title">
<a href="#216" onclick="clearInputFields('searchbox');">RESET</a>
</div>

关于javascript - 如何重置过滤列表搜索框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com