HTML可搜索的动态列表,突出显示结果

时间:2019-10-26 13:57:28

标签: javascript html dynamic html-lists searchable

在我的工作中,我有一个Excel工作表,其中包含成千上万条评论,在验证一些文档后需要查找并发送。由于所有的表格编号,部分,错误等信息,有时并不容易找到正确的注释,有时它们很长。

我从此示例开始,我想在其中添加搜索多个单词或字母(以空格分隔)的选项,以及突出显示结果或将其标记为粗体的选项。所有这一切,同时保持了列表的动态性。意味着搜索结果会被过滤。

非常感谢! 感谢您的帮助:)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  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;
}
</style>
</head>
<body>

<h2>My Comments</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for comments.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">Form 3001: Section A, name incorrect + Section B, date missing + Section F, title incorrect</a></li>
  <li><a href="#">Form 3001: Section A, Street number missing + Section E, date should be before 2019 + Section N, title missing</a></li>

  <li><a href="#">Form 3001: Section F, date of birth incorrect + Section H, last name incorrect + Section D, date missing</a></li>
  <li><a href="#">Form 3003: Section A, name incorrect + Section B, date missing + Section F, title incorrect</a></li>

  <li><a href="#">Form 3003: Section A, Street number missing + Section E, date should be before 2019 + Section N, title missing</a></li>
  <li><a href="#">Form 3003: Section F, date of birth incorrect + Section H, last name incorrect + Section D, date missing</a></li>
</ul>

<script>
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";
        }
    }
}
</script>

</body>
</html>

0 个答案:

没有答案