javascript过滤器/搜索Html列表

标签 javascript

这是一个简单的程序,用于从 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/

相关文章:

javascript - Jquery将文本框中的值与按钮​​相乘

javascript - 使用日期时间选择器格式时,它会增加 5 年

javascript - 通过自定义属性进行表单验证

javascript - HTML/JS 打开一个窗口到 C$ 驱动器

javascript - 调试 Firebase 客户端连接

javascript - 检查 Javascript 是否存在滚动条?

javascript - Vue Webpack 构建破坏了 Bulma CSS

javascript - 如何降低根据数据类型回调的函数的复杂性?

javascript从回调返回给变量赋值(计时)

javascript - 当 TextInput 获得焦点时,如何自动将窗口从键盘后面滑出?