JQ模糊匹配(类似百度搜索等)

CSS代码
<style type="text/css">   

        #div_txt {  
            position: relative;  
            width: 200px;  
            margin: 0 auto;  
            margin-top: 40px;  
        }  

        #txt1 {  
            width: 99%;  
        }  

        #div_items {  
            position: relative;  
            width: 100%;  
            height: 200px;  
            border: 1px solid #66afe9;  
            border-top: 0px;  
            overflow: auto;  
            display: none;  
        }  

        .div_item {  
            width: 100%;  
            height: 20px;  
            margin-top: 1px;  
            font-size: 13px;  
            line-height: 20px;  
        }  
    </style>  
$(function(){ 
    //移入移出效果  
    $(".div_item").hover(function () {  
        $(this).css('background-color', '#1C86EE').css('color', 'white');  
    }, function () {  
        $(this).css('background-color', 'white').css('color', 'black');  
    }); 
    //隐藏列表框  
    $("body").click(function () {  
        $("#div_items").css('display', 'none');  
    });   

    //文本框输入  
    $("#txt1").keyup(function () {  
        $("#div_items").css('display', 'block');//只要输入就显示列表框  

        if ($("#txt1").val().length <= 0) {  
            $(".div_item").css('display', 'block');//如果什么都没填,跳出,保持全部显示状态  
            return;  
        }  

        $(".div_item").css('display', 'none');//如果填了,先将所有的选项隐藏  

        for (var i = 0; i < $(".div_item").length; i++) {  
            //模糊匹配,将所有匹配项显示[匹配首字母]  
            //if ($(".div_item").eq(i).text().substr(0, $("#txt1").val().length) == $("#txt1").val()) {  
            //匹配全部文字
            if($(".div_item").eq(i).text().indexOf( $("#txt1").val() )!=-1 ){
                $(".div_item").eq(i).css('display', 'block');  
            }  
        }  
    });  

    //项点击  
    $(".div_item").click(function () {  
        $("#txt1").val($(this).text());  
    }); 
})
<div id="div_txt">  
        <!--要模糊匹配的文本框-->  
        <input type="text" id="txt1" />  
        <!--模糊匹配窗口-->  
        <div id="div_items">  
            <div class="div_item">周杰伦</div>  
            <div class="div_item">周杰</div>  
            <div class="div_item">林俊杰</div>  
            <div class="div_item">林宥嘉</div>  
            <div class="div_item">林妙可</div>  
            <div class="div_item">唐嫣</div>  
            <div class="div_item">唐家三少</div>  
            <div class="div_item">唐朝盛世</div>  
            <div class="div_item">奥迪A4L</div>  
            <div class="div_item">奥迪A6L</div>  
            <div class="div_item">奥迪A8L</div>  
            <div class="div_item">奥迪R8</div>  
            <div class="div_item">宝马GT</div>  
        </div>
    </div>  

这里写图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若水印象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值