【问题标题】:Search bar is only searching on first p tag搜索栏只搜索第一个 p 标签
【发布时间】:2020-05-13 12:39:50
【问题描述】:

我正在尝试使用 .net core MVC 在我的 webb 应用程序中实现搜索栏。

我在网上找到了一些代码,但问题是它只会根据第一个 p-tag 进行搜索。 在将显示设置为无之前,无法弄清楚如何使其搜索所有 p-tags。

这是我的 JS 函数:

function myFunction() {
    var input, filter, ul, li, p, 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++) {
        p = li[i].getElementsByTagName("p")[0];
        txtValue = p.textContent || p.innerText;
        if (txtValue.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}

这是我的cshtml:

<ul id="myUL" style="display: inline;
                    list-style: none;">
    @await foreach (var logopedist in Model)
    {
            <li class="lijst" style="margin-top: 10px;
        padding-left: 10px;
        background-color: #1d71b8;
        color: #ffffff;
        font-size: 10pt;
        padding-right: 10px;
        padding-bottom: 5px;
        padding-top: 5px;">
                <p style="font-size:14px; font-weight:bold;">@logopedist.Naam, @logopedist.PraktijkNaam</p>
                <p>@logopedist.Adres</p>
                <p>@logopedist.Email</p>
                <p>@logopedist.Mobiel</p>
            </li>
        }

【问题讨论】:

  • 这不是一个尖锐的问题。请点击编辑,然后点击[&lt;&gt;]sn-p 编辑器并发布minimal reproducible example,仅包含相关的 HTML 和脚本
  • 请使用 CSS 样式表而不是内联 CSS

标签: javascript html


【解决方案1】:

我推荐这个版本

window.addEventListener("load", function() {
  document.getElementById("myInput").addEventListener("input", function() {
    const filter = this.value.toUpperCase();
    [...document.querySelectorAll("#myUL li")].forEach(li =>  {
      const txtValue = (li.textContent || li.innerText).toUpperCase();
      li.classList.toggle("hide",txtValue.indexOf(filter) === -1)
    });
  })
})
#myUL {
  display: inline;
  list-style: none;
}

#myUL li {
  margin-top: 10px;
  padding-left: 10px;
  background-color: #1d71b8;
  color: #ffffff;
  font-size: 10pt;
  padding-right: 10px;
  padding-bottom: 5px;
  padding-top: 5px;
}

.lijst p {
  font-size: 14px;
  font-weight: bold;
}

.hide { display:none }
<input id="myInput" />

<ul id="myUL" style="">
  <li class="lijst">
    <p>NaamA PraktijkB</p>
    <p>AdresA</p>
    <p>logopedistA@Email.com</p>
    <p>0612345678</p>
  </li>
  <li class="lijst">
    <p>NaamC PraktijkD</p>
    <p>AdresC</p>
    <p>logopedistB@Email.com</p>
    <p>0687654321</p>
  </li>

【讨论】:

  • 我已经实现了您的确切代码,但是每当我在输入框中输入时,什么都没有发生。我尝试稍微更改 JS,但无法使其正常工作。在此之前,我在输入标签中使用了这个“onkeyup="myFunction()”。我还像你说的那样将我所有的内联 css 移动到 css 样式表中。
  • 谢谢我找到了解决方案。我不得不从 [...document.querySelectorAll 中删除 [...] 不知道为什么。但这解决了它,非常感谢!
  • [...someselector].forEach 适用于没有 htmlcollection.forEach 可用的浏览器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 2011-06-25
相关资源
最近更新 更多