Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 过滤搜索<;ul>;_Javascript_Jquery_Search_Html Lists_Textinput - Fatal编程技术网

Javascript 过滤搜索<;ul>;

Javascript 过滤搜索<;ul>;,javascript,jquery,search,html-lists,textinput,Javascript,Jquery,Search,Html Lists,Textinput,我还有一个用户列表: <ul> <li class="thumb selectable arrow light" style="margin-bottom:-5px;" data-image="http://cdn.tapquo.com/lungo/icon-144.png"> <strong class="name">Peter <font data-count="0" style="position:relative;top:-2px;"> &

我还有一个用户列表:

<ul>
<li class="thumb selectable arrow light" style="margin-bottom:-5px;"
data-image="http://cdn.tapquo.com/lungo/icon-144.png">
<strong class="name">Peter <font data-count="0" style="position:relative;top:-2px;"> </font></strong> 
<small class="description">Hi!</small> 
</li>
...
</ul>
  • 彼得 你好
  • ...

我想要的是每次写一封信时输入一个文本,只显示以该信开头的用户或他们可能有名字的用户。我能做什么?这是jquery,但不是…

这里有一个
输入,它根据纯JavaScript中的值过滤
。它的工作原理是处理
onkeyup
,然后获取
  • 并将其内部元素
    .name
    与过滤器文本进行比较

    var input=document.getElementById('input');
    input.onkeyup=函数(){
    var filter=input.value.toUpperCase();
    var lis=document.getElementsByTagName('li');
    对于(变量i=0;i
    因此,在无序列表中有一组列表项,如下所示

    <div class="some-input-field-class">
       <input type="text" name="filter" id="filter">
       <label for="filter">Filter Names</label>
    </div>
    
    <ul class="my-user-collection">
      <li class="thumb selectable arrow light" style="margin-bottom:-5px;"
      data-image="http://cdn.tapquo.com/lungo/icon-144.png">
      <strong class="name">Peter <font data-count="0" style="position:relative;top:-2px;"> </font></strong> 
      <small class="description">Hi!</small> 
      </li>
      ...
    </ul>
    
    函数以类为目标遍历li,遍历数组,因为我们使用querySelectorAll而不是getElementByID-1意味着没有对手

    function filterNames(e) {
      const text = e.target.value.toLowerCase();
      document.querySelectorAll('.selectable').forEach(
        function(name) {
          let item = name.firstChild.textContent;
          if (item.toLowerCase().indexOf(text) != -1) {
            name.style.display = 'block';
          } else {
            name.style.display = 'none';
          }
        }
      );
    }
    

    我真的没有得到你想要的。加上你做了什么?一直到我在沙滩上。您有一个用户列表和一个文本输入。当我们开始打字时,你希望匹配的用户出现在我完全困惑的地方。你能详细说明你想要达到的目标吗?你想要一个
    input
    字段,它有一个监听器,并检查插入其中的每个字母是否以字母开头?那么你说的是btevfik,我有一个用户列表和一个输入,每当用户输入一个不包含这些字母的字母时,这些字母就会消失,并且只保留匹配的字母。作为这个产品过滤器的一个例子,不必使用jquerymobile,可能应该使用
    display='list item'请扩展答案以搜索与名字或姓氏匹配的名称。应显示搜索是否匹配任何名称(第一个或最后一个)。在上述情况下为“Andrew”或“Hi”。要在单词中的任意位置搜索匹配项,请改用
    if(name.toUpperCase().indexOf(filter)!=-1)
    <代码>-1
    用于表示没有匹配项。
    // Load the event listener
    document.querySelector('#filter').addEventListener('keyup', filterNames);
    
    function filterNames(e) {
      const text = e.target.value.toLowerCase();
      document.querySelectorAll('.selectable').forEach(
        function(name) {
          let item = name.firstChild.textContent;
          if (item.toLowerCase().indexOf(text) != -1) {
            name.style.display = 'block';
          } else {
            name.style.display = 'none';
          }
        }
      );
    }