码迷,mamicode.com
首页 > 其他好文 > 详细

精准定位

时间:2016-02-25 11:39:27      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

function LookMatch(){
     
}
LookMatch.init = function(seach,els){
 var that = new LookMatch();
     that._setElement(seach,els)
}
LookMatch.prototype._setElement = function(seach,ul){
    this.seachEl = seach;
    this.ul = ul;
    this.backEls = this.ul.clone(true);
    var that = this;
     this.ul.on(‘click‘,‘li‘,function(){
         
         that.seachEl.val($(this).text());  
         that.ul.hide();
         that.restore();
    });
    this.seachEl.on(‘focus‘,function(){
        console.log(‘ss‘);
       that.ul.show(); 
    });
    this.seachEl.on(‘keyup‘,function(){
         var q = $(this).val();
         if (!q) {
            that.restore();
            return;
         }
         var arr = [];
         that.ul.find(‘li‘).each(function (i, e) {
             var text = $(this).text();
             var indexof = text.indexOf(q);
             if (indexof != -1) {
                 arr.push(e);
             }
         });
         if (!arr.length) {
             return;
         }
         $.each(arr.reverse(), function (i, e) {
            that.ul.prepend(e);
         });
    })
}
LookMatch.prototype.restore=function(){
    this.ul.empty().html(this.backEls.html());
}
// 搜索框,列表
LookMatch.init($(‘#seachinput‘),$(‘#uu‘));

*

精准定位

标签:

原文地址:http://www.cnblogs.com/change-oneself/p/5216190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!