【问题标题】:How to display list items according by text field search?如何根据文本字段搜索显示列表项?
【发布时间】:2021-01-11 07:01:02
【问题描述】:

我有一个列表,其中包含默认情况下需要隐藏并在搜索字段为空白/退格时再次隐藏的结果。如果未找到结果,则显示消息或结果表明“未找到匹配项”。我之前有一个帖子有类似的问题,但不得不放弃它,因为它是使用引导程序制作的,与我的 Wordpress 主题设置冲突。

<!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 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>

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

$(document).ready(function() {
  $("#myUL li").hide();
  $('#contact').hide();
});

</script>

</body>
</html>

【问题讨论】:

  • 你能把问题说得更简洁一点
  • 更新并简化了我的问题。我正在寻找的东西的列表点形式。
  • 您可能仍想改写并集中您的帖子,现在的答案是“是”

标签: javascript html jquery css wordpress


【解决方案1】:

此解决方案使用您的代码的略微重构版本,包括:

  • “keyup”事件侦听器(而不是内联事件处理程序)
  • 一个名为.hidden的新CSS选择器
  • 一个名为no-results的新HTML元素
  • 两个处理列表项的辅助函数

事件监听器和 CSS 选择器有助于页面上的关注点分离。 (样式由 CSS 完成,而不是由 JavaScript 手动完成,事件由 JavaScript 单独处理,而不是包含在页面的标记中。)no-results 元素和辅助函数只是个人偏好,但我认为他们让事情更整洁。

请参阅代码中的 cmets 以获得进一步的解释。

$(document).ready(function() {

  const

    // Identifies DOM elements
    input = document.getElementById("myInput"),
    noResults = document.getElementById("no-results"),
    listItems = Array.from(document.querySelectorAll("#myUL li")),

    // Defines a function to get uppercased li text
    getLiUpperText = (li) => {
      const
        a = li.getElementsByTagName("a")[0],
        text = a.textContent || a.innerText
      liUpperText = text.toUpperCase();
      return liUpperText;
    },

    // Defines a function to hide all list items
    hideListItems = () =>
      listItems.forEach((li) => li.classList.add("hidden"));


  // Hides everything initially (using CSS instead of manual styling)
  noResults.classList.add("hidden");
  hideListItems();

  // Calls inputListener on keyup
  input.addEventListener("keyup", inputListener);


  // Defines inputListener
  function inputListener(event) {

    hideListItems(); // Tentatively hides all names
    const inputUpperText = event.target.value.toUpperCase();
    if (inputUpperText === "") {
      return; // Quits early if input is empty
    }
    noResults.classList.remove("hidden"); // Tentatively shows 'no results'

    for (let li of listItems) {
      if (getLiUpperText(li).includes(inputUpperText)) {
        li.classList.remove("hidden"); // Shows item
        noResults.classList.add("hidden"); // Hides 'no results'
      }
    }
  }

});
* {
  box-sizing: border-box;
}

.hidden {
  /* new ruleset for hiding elements */
  display: none;
}

#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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h2>My Phonebook</h2>
<input type="text" id="myInput" placeholder="Search for names.." title="Type in a name">

<div>
  <p id="no-results">No match found</p>
  <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>

【讨论】:

  • 我在我的 Wordpress 页面中尝试过,但结果似乎没有被隐藏。
  • 上面的 sn-p 是否适合您? (我只在最新的 Chrome 浏览器中对此进行了测试,因此您的浏览器的行为可能有所不同。)
【解决方案2】:

您可以创建一个函数来检查输入值是否存在以隐藏/显示 UL。 在输入更改和页面加载时调用此函数

checkInputValueAndHideList()

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";
        }
    }
    
    checkInputValueAndHideList()
    
}

function checkInputValueAndHideList() {
    const ul = document.getElementById("myUL");
    const input = document.getElementById("myInput");
    const noResult = document.getElementById("no-result");

//        console.log('input.value', input.value) 
    
    if(!input.value) {
        ul.style.display = 'none'
        noResult.style.display = 'block'
    } else {
        ul.style.display = 'block'
        noResult.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;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>

<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>

<div id="no-result">
No match found
</div>



</body>
</html>

【讨论】:

  • 尝试在输入中输入“RR”(不存在!),它应该显示“未找到匹配项”
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 2013-02-26
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 2018-10-14
相关资源
最近更新 更多