gpt4 book ai didi

javascript过滤器/搜索Html列表

转载 作者:行者123 更新时间:2023-12-03 06:58:13 26 4
gpt4 key购买 nike

这是一个简单的程序,用于从 html 列表中搜索一个项目,它工作得很好,但是如何通过添加几行代码来显示一条消息,如未找到任何内容或未找到结果,如果输入与来自列表 ?

function myFunction(){
let input = document.getElementById('myInput').value.trim();
let ul = document.getElementById("myUL");
let li = ul.getElementsByTagName("li");
let anch = document.getElementsByTagName('a');
let res = document.getElementById('result');
for(let i = 0 ; i < anch.length ; i++){
if(anch[i].innerText.toLowerCase().indexOf(input) > -1){
anch[i].style.display="";
anch[i].style.color="red";
}else{
anch[i].style.display="none";
}
}
}
<style>
#myInput {
background-image: url('/css/searchicon.png'); /* Add a search icon to input */
background-position: 10px 12px; /* Position the search icon */
background-repeat: no-repeat; /* Do not repeat the icon image */
width: 100%; /* Full-width */
font-size: 16px; /* Increase font-size */
padding: 12px 20px 12px 40px; /* Add some padding */
border: 1px solid #ddd; /* Add a grey border */
margin-bottom: 12px; /* Add some space below the input */
}

#myUL {
/* Remove default list styling */
list-style-type: none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd; /* Add a border to all links */
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6; /* Grey background color */
padding: 12px; /* Add some padding */
text-decoration: none; /* Remove default text underline */
font-size: 18px; /* Increase the font-size */
color: black; /* Add a black text color */
display: block; /* Make it into a block element to fill the whole list */
}
#myUL li a:hover:not(.header) {
background-color: #eee; /* Add a hover effect to all links, except for headers */
}
</style>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=0.1,shrink-to-fit=no">
<link type="text/css" href="/css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>Search</title>
</head>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
</ul>

最佳答案

像这样尝试:

function myFunction(){
let input = document.getElementById('myInput').value.trim();
let ul = document.getElementById("myUL");
let li = ul.getElementsByTagName("li");
let anch = document.getElementsByTagName('a');
let res = document.getElementById('result');
let noresult = document.getElementById('noresult');
let resfound = false;
for(let i = 0 ; i < anch.length ; i++){
if(anch[i].innerText.toLowerCase().indexOf(input.toLowerCase()) > -1){
anch[i].style.display="";
anch[i].style.color="red";
resfound = true;
}else{
anch[i].style.display="none";
}
}

if(resfound) {
noresult.style.display = "none"
} else {
noresult.style.display = "inline-block"
};
}
<style>
#myInput {
background-image: url('/css/searchicon.png'); /* Add a search icon to input */
background-position: 10px 12px; /* Position the search icon */
background-repeat: no-repeat; /* Do not repeat the icon image */
width: 100%; /* Full-width */
font-size: 16px; /* Increase font-size */
padding: 12px 20px 12px 40px; /* Add some padding */
border: 1px solid #ddd; /* Add a grey border */
margin-bottom: 12px; /* Add some space below the input */
}

#myUL {
/* Remove default list styling */
list-style-type: none;
padding: 0;
margin: 0;
}
#myUL li a {
border: 1px solid #ddd; /* Add a border to all links */
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6; /* Grey background color */
padding: 12px; /* Add some padding */
text-decoration: none; /* Remove default text underline */
font-size: 18px; /* Increase the font-size */
color: black; /* Add a black text color */
display: block; /* Make it into a block element to fill the whole list */
}
#myUL li a:hover:not(.header) {
background-color: #eee; /* Add a hover effect to all links, except for headers */
}
</style>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=0.1,shrink-to-fit=no">
<link type="text/css" href="/css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title>Search</title>
</head>
<body>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."><span id="noresult" style="display:none;">No results found</span>
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
</ul>
noresult默认隐藏文本输入后添加。如果没有找到结果则显示,否则再次隐藏。

关于javascript过滤器/搜索Html列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64955071/

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