详解jQuery如何实现模糊搜索

所属分类: 网络编程 / JavaScript 阅读数: 1789
收藏 0 赞 0 分享

如何实现 模糊搜索 当我们浏览网页的时候,通常能看到搜索栏,这大大的提高了我们获取数据的目的性。
那如何去实现一个简单的模糊搜索 框呢,以下案例获取能给你一点思路。

以下案例,可以实现当按键按下时,自动检索匹配数据

基本css 样式

.row {

​    height: 80px;

​    */\* line-height: 80px; \*/*

​    text-align: left;

​    line-height: 80px;

​    padding-top: 5px;

​    margin-bottom: 10px;

​  }

​  .inh {

​    width: 70px;

​    height: 70px;

​    border: 1px solid blanchedalmond;

​    border-radius: 5px;

​    line-height: 70px;

​    text-align: center;

​    margin-right: 30px;

​  }

​  img {

​    width: 100%;

​    height: 100%;

​  }

基本的html 样式

<div class="search_box"><i class="fa fa-arrow-left ftop"></i>
  <form action="#">
    <input type="text" id="index-to" placeholder="请输入搜索内容" autofocus onfocus="autoPlays">
    <i class="fa fa-times fa=1gt rege"></i>
  </form>
</div>


<div class="search_content search_default">
  <ul id="view-to"></ul>
</div>
</div>

初始样式图如下:

/**
 * 自己创建一个商品数据集合
 * 点击分类时实现商品的切换
 * 切换之后已经选择好的数量需要记录
*/
var arrAllProducts = [
  {
    type: "炒菜",
    products: [
      { id: 10001, name: "土猪肉烧红薯", img: "http://recipe1.hoto.cn/pic/recipe/l/ff/4f/1134591_e480ee.jpg", price: 26.00, desc: "红薯与肉香交互辉映,肥而不腻、酥而不碎、甜而不粘、浓而不咸。" },
      { id: 10002, name: "红烧虾园子", img: "http://recipe1.hoto.cn/pic/recipe/l/c3/66/1140419_19dbfb.jpg", price: 28.00, desc: "传统的《桂花酒酿圆子》有现成的卖,自己做也是简单方便口味很不错" },
      { id: 10003, name: "宫保鸡丁", img: "http://recipe0.hoto.cn/pic/recipe/g_148/6a/da/252522_0d88b3.jpg", price: 20.00, desc: "川菜馆必点" }
    ]
  },
  {
    type: "商务套餐",
    products: [
      { id: 20001, name: "荷叶饭", img: "http://recipe0.hoto.cn/pic/recipe/g_148/72/61/1073522_c9b4af.jpg", price: 12.00, desc: "好吃的荷叶饭" },
      { id: 20002, name: "奢华版荷叶饭", img: "http://recipe0.hoto.cn/pic/recipe/g_148/40/f8/849984_c84667.jpg", price: 15.00, desc: "精装版" }
    ]
  },
  {
    type: "主食",
    products: [
      { id: 30001, name: "芝麻拌苦瓜", img: "http://res.hoto.cn/5c7787ea0135db3ab01db0d5.jpg!default", price: "12.00", desc: "这款燕麦南瓜饼,外皮软糯,内馅香甜" }
    ]
  },
  {
    type: "其他",
    products: [
      { id: 40001, name: "苏格兰蛋", img: "http://recipe0.hoto.cn/pic/recipe/l/2a/67/1140522_c0045b.jpg", price: "25.80", desc: "据说这叫苏格兰蛋。其实油炸的我吃得少做的更少" }
    ]
  }
]
 
// 封装 模糊搜索的方法
function autoPlays(x) {
    x.style.border = '5px soild blue'
  }
  
  $(function () {
    var search_input = $(".search_box input"),
      search_content = $(".search_content");
    $(search_input).on("keyup", function () {
      if (search_input.val() == '') {
        $(search_content).show();
      }
      // $(".search_content li:contains(" + search_input.val().trim() + ")").show();
      // $(".search_content li:not(:contains(" + search_input.val().trim() + "))").hide();


      //第二中方法
      $(".search_content li").hide().filter(":contains(" + search_input.val().trim() + ")").show();
    });
  });
  $(".ftop").click(function () {
    history.back(1);
  })

  $('#index-to').keyup(function () {
    var search_input = $(".search_box input")

    if (search_input.val() != '') {
      $('.rege').css({
        display: 'block'
      })
      $('#view-to').css({
        display: 'block'
      })
    }
    else {
      $('#view-to').css({
        display: 'none'
      })
      $('.rege').css({
        display: 'none'
      })
    }

  })
  $('.rege').click(function () {
    $('#index-to').val('');
    $('#view-to').css({
      display: 'none'
    })
    $(this).css({
      display: 'none'
    })
  })
  // 遍历arrAllProducts 数组
  for (var key in arrAllProducts) {
    console.log(arrAllProducts[key].products)
    $.each(arrAllProducts[key].products, function (i, value) {
      var oLi = "<li class='row'><img src='' class='inh' alt='图片加载失败'><a href='javascript:;'>" + value.name + "</a></li>";
      console.log(value.img+'nnnnnimg')
      var oLis = $(oLi);
      oLis.appendTo($("#view-to"))
      let uuu = $('.inh')
      uuu[i].src = value.img
      console.log(value.name)
    })
  }

搜索效果图如下:

以上所述是小编给大家介绍的jQuery如何实现模糊搜索详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

jQuery控制input只能输入数字和两位小数的方法

这篇文章主要介绍了jQuery控制input只能输入数字和两位小数的相关知识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Vue模板语法中数据绑定的实例代码

这篇文章主要介绍了Vue模板语法中数据绑定的实例代码,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

详解 微信小程序开发框架(MINA)

小程序使用的是MINA框架,目的是通过简单、高效的方式让开发者可以在微信中开发具有原生App体验的服务。 这篇文章主要介绍了微信小程序开发框架(MINA),需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery实现的点击显示隐藏下拉菜单功能完整示例

这篇文章主要介绍了jQuery实现的点击显示隐藏下拉菜单功能,结合完整实例形式分析了jQuery事件响应及页面元素属性动态操作简单实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

angular4应用中输入的最小值和最大值的方法

这篇文章主要介绍了angular4应用中输入的最小值和最大值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

150行代码带你实现微信小程序中的数据侦听

在这篇文章中, 我将用150行代码, 手把手带你打造一个小程序也可以使用的侦听器,感兴趣的朋友跟随小编一起看看吧
收藏 0 赞 0 分享

javascript异步编程的六种方式总结

这篇文章主要介绍了javascript异步编程的六种方式总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

JS实现的自定义map方法示例

这篇文章主要介绍了JS实现的自定义map方法,结合实例形式分析了javascript自定义map相关的json数组定义、遍历、添加、删除、读取等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

微信小程序云开发(数据库)详解

使用云开发开发微信小程序、小游戏,无需搭建服务器,这篇文章主要为大家详细介绍了微信小程序云开发数据库,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JS简单数组排序操作示例【sort方法】

这篇文章主要介绍了JS简单数组排序操作,结合实例形式分析了javascript使用sort方法进行数组排序的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多