简体   繁体   English

如何使用Javascript / jQuery以及不区分大小写的文本或搜索字段动态显示/隐藏div(在KeyUp,iTunes样式上)

[英]How can I show/hide divs dynamically (on KeyUp, iTunes style) using Javascript/jQuery and a text or search field with case insensitive

I set out on a journey to create an iTunes-like search using Javascript. 我开始着手使用Javascript创建类似iTunes的搜索。 I learned about jQuery, and with some help from people on StackOverflow, I was successful . 我学习了jQuery,在StackOverflow上的人们的帮助下,我获得了成功

I've come back here to share with you a very simple way to create a dynamic hide/show list based on the user input. 我回到这里与您分享一种基于用户输入创建动态隐藏/显示列表的简单方法。

Let's search!

The entirety of the tutorial code can be found here . 可以在此处找到完整的教程代码。

And a JSFiddle for it is here ! 它的JSFiddle就在这里

First, create a simple Div Layout with some text in the divs and search bar above it. 首先,创建一个简单的Div布局,其中的div和搜索栏中有一些文本。

    <div class="search_bar">
      <form><!--The Field from which to gather data-->
        <input id="searchfield" type="text" onclick="value=''" value="Case Sensitive Search">
      </form>
    </div>
    <!--Containers With Text-->
    <div class="container">
      <div class="container_of_hc">
        <div class="horizontal_containers">Cat</div>
        <div class="color">Black</div>
        <div class="color">White</div>
        <div class="color">Orange</div>
      </div>
      <div class="horizontal_containers">Dog</div>
      <div class="horizontal_containers">Rat</div>
      <div class="horizontal_containers">Zebra</div>
      <div class="horizontal_containers">Wolf</div>
    </div>

CSS: CSS:

      .container {
        width: 100%;
      }
      .horizontal_containers {
        height:10%;
        border: solid 3px #B30015;
        font-size: 45px;
        text-align: center;
      }

Second, you will make a script utilizing jQuery. 其次,您将使用jQuery创建一个脚本。 Remember the title says this is a Dynamic Search, meaning (for us) we want to update the search with each key typed: 记住标题说这是一个动态搜索,意思是(对我们来说)我们想要用键入的每个键更新搜索:

$("#searchfield").keyup(function() { 

Note: Need a selector refresher? 注意:需要选择器复习吗?

Then we will set a variable to the value in #searchfield: 然后我们将变量设置为#searchfield中的值:

  var str = $("#searchfield").val();  //get current value of id=searchfield

To ensure we show all the divs in our list when there is nothing in the searchfield we create an if statement based on the length of our new variable (str): 为了确保在搜索字段中没有任何内容时我们显示列表中的所有div,我们根据新变量(str)的长度创建一个if语句:

  if (str.length == 0) {
    //if searchfield is empty, show all
    $(".horizontal_containers").show();
  }

Last, we do the actual hiding of the divs if the length of str is not 0: 最后,如果str的长度不为0,我们会实际隐藏div:

  else {
    //if input contains matching string, show div
    //if input does not contain matching string, hide div
    $("div:contains('" + str + "').horizontal_containers").show();
    $("div:not(:contains('" + str + "')).horizontal_containers").hide();
  }
});

The div:contains() and div:not(:contains()) statements are what set the conditions. div:contains()div:not(:contains())语句是设置条件的。 It's essentially an if statement. 它本质上是一个if语句。 They search the text contained within the div, not the div attributes. 他们搜索div中包含的文本,而不是div属性。 If you want to search a deeper div structure you can use more than one selector in the script's jQuery statements like so: 如果你想搜索更深层的div结构,你可以在脚本的jQuery语句中使用多个选择器,如下所示:

if (str.length == 0) {
//if searchfield is empty, show all
  $(".container .color").show();
  } else {
    //if input contains matching string, show div
    //if input does not contain matching string, hide div
    $(".container div:contains('" + str + "').color").show();
    $(".container div:not(:contains('" + str + "')).color").hide();
  }

Replace the script statement you already have to give it a try. 替换您已经拥有的脚本语句尝试一下。

Note: The nesting structure of your divs must match that in your selector. 注意:div的嵌套结构必须与选择器中的嵌套结构相匹配。

And that's essentially it. 而这基本上就是它。 If you have tips to improve this, know how to change it to a case insensitive search , or anything else you can think of, please let me know! 如果您有改进的提示,知道如何将其更改为不区分大小写的搜索 ,或者您能想到的任何其他内容,请告诉我们!

Thanks to MrXenoType I have learned case insensitivity for the :contains function. 感谢MrXenoType,我学会了:contains函数的大小写不敏感。

To create a case insensitive search for this project simply add: 要为此项目创建不区分大小写的搜索,只需添加:

$.expr[":"].contains = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

This creates a pseudo for the contains function. 这为contains函数创建了一个伪。 Place this code above your other script (within the same script) to make true for only this script. 将此代码放在您的其他脚本(在同一脚本中)之上,以便仅对此脚本生效。

So good to see Nick was successful on this experiment. 很高兴看到Nick在这个实验中取得了成功。 good job on learning how to do it :) 学习如何做好:)

Just in case you haven't encountered this jquery plugin, you might want to take a look at it too it's called Quick search. 如果你没有遇到过这个jquery插件,你可能也想看一下它叫做快速搜索。

https://github.com/riklomas/quicksearch https://github.com/riklomas/quicksearch

And I've used it on numerous pages and it works like a charm. 我已经在很多页面上使用它,它就像一个魅力。 example: http://fedmich.com/works/types-of-project.htm 例如: http//fedmich.com/works/types-of-project.htm

Try: 尝试:

$.expr[":"].contains_nocase = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

for adding a :contains_nocase() selector with jQuery 1.8 用jQuery 1.8添加:contains_nocase()选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM