javascript - Js 实时搜索模式

标签 javascript html css livesearch

function livesearch() {
    // Declare variables
    var input, filter, ul, li, a, i;
    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];
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
#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 */
}
    <input type="text" id="myInput" onkeyup="livesearch()" placeholder="Search for names..">

    <ul id="myUL">
        <li><a href="#">name</a></li>
        <li><a href="#">two word</a></li>
        <li><a href="#">three words name</a></li>
        <li><a href="#">four words name example</a></li>
    </ul>

大家好 我正在使用 html css 和 js 进行实时搜索 当我搜索确切的模式时,一切都很好 但我正在寻找显示不规则搜索的内容。 比如当有人搜索“四个名字”或“单词示例”时,结果显示“四个单词名称示例”。谁能帮我制作这个搜索模式?

最佳答案

希望对你有帮助!

function livesearch() {
  // Declare variables
  var input, filter, ul, li, a, i;
  input = document.getElementById('myInput');
  filter = input.value.toUpperCase();
  filter = filter.split(' ');
  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++) {
    var sw = 1;
    a = li[i].getElementsByTagName("a")[0];
    for (var jj = 0; jj < filter.length; jj++) {
      if (a.innerHTML.toUpperCase().indexOf(filter[jj]) == -1) {
        sw = 0;
        break;
      }
    }
    if (sw) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
#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 */
}
<input type="text" id="myInput" onkeyup="livesearch()" placeholder="Search for names..">

<ul id="myUL">
  <li><a href="#">name</a></li>
  <li><a href="#">two word</a></li>
  <li><a href="#">three words name</a></li>
  <li><a href="#">four words name example</a></li>
</ul>

关于javascript - Js 实时搜索模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202024/

相关文章:

javascript - 使用javascript提取起始字符串和结束字符串之间的字符串

JavaScript const 作用域和花括号

javascript - onclick 在 jquery 中不起作用

html - Firefox 框阴影在最新版本中更亮

javascript - 等待CSS过渡

html - 使用来自不同服务器的 css 链接到托管字体

javascript - 一个盒子里只放一个动子

javascript - Chartjs 图例样式

Javascript 缩小缓存问题

html - 缓慢呈现的 Html 页面