用js实现输入提示(自动完成)
创始人
2024-07-22 04:21:56
0

以前也写过一个jQuery的这种插件 ,但是很多地方根本不用jQuery,这个功能也有很多其它库支持,但是为了用这个功能而加载很多js插件,这样效率明显下降了很多,而且这个东西平时也很常用,所以一决心自己写了个相对比较独立的。

完成有以下功能:

输入字符会把以输入字符开头的提示出来。

支持上下方向键选择提示选项,支持循环

支持绑定一个数组提示,支持ajax传递输入框值请求数据。

支持多个选择的dom元素一块绑定数据实现输入提示。各dom元素也可以单独绑定自己的数据实现输入提示,互不影响。

支持中文。

首先是js的核心部分,其各部分都有较详细的说明,代码如下:

view source print ?

 

  1.   ;( function (window){    
  2.  
  3.   /* 插件开始 */    
  4.  
  5.   var autoComplete= function (o){    
  6.  
  7.        var handler=( function (){    
  8.  
  9.            var handler= function (e,o){ return new handler.prototype.init(e,o); }; /* 为每个选择的dom都创建一个相对应的对象,这样选择多个dom时可以很方便地使用 */    
  10.  
  11.            handler.prototype={    
  12.  
  13.                e: null , o: null , timer: null , show:0, input: null , popup: null ,    
  14.  
  15.                init: function (e,o){ /* 设置初始对象 */    
  16.  
  17.                    this .e=e, this .o=o,    
  18.  
  19.                    this .input= this .e.getElementsByTagName( this .o.input)[0],    
  20.  
  21.                    this .popup= this .e.getElementsByTagName( this .o.popup)[0],    
  22.  
  23.                    this .initEvent(); /* 初始化各种事件 */    
  24.  
  25.               },    
  26.  
  27.               match: function (quickExpr,value,source){ /* 生成提示 */    
  28.  
  29.                   var li = null ;    
  30.  
  31.                   for ( var i in source){    
  32.  
  33.                       if ( value.length>0 && quickExpr.exec(source[i])!= null ){    
  34.  
  35.                           li = document.createElement( 'li' );    
  36.  
  37.                           li.innerHTML = '' +source[i]+ 'a>' ;    
  38.  
  39.                          this .popup.appendChild(li);    
  40.  
  41.                       }    
  42.  
  43.                   }    
  44.  
  45.                   if ( this .popup.getElementsByTagName( 'a' ).length)    
  46.  
  47.                       this .popup.style.display= 'block' ;    
  48.  
  49.                   else    
  50.  
  51.                        this .popup.style.display= 'none' ;    
  52.  
  53.                },    
  54.  
  55.                ajax: function (type,url,quickExpr,search){ /* ajax请求远程数据 */    
  56.  
  57.                   var xhr = window.ActiveXObject ? new ActiveXObject( "Microsoft.XMLHTTP" ) : new XMLHttpRequest();    
  58.  
  59.                    xhr.open(type,url, true ); /* 同异步在此修改 */    
  60.  
  61.                    xhr.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );    
  62.  
  63.                   var that= this ;    
  64.  
  65.                   xhr.onreadystatechange = function (){    
  66.  
  67.                       if (xhr.readyState==4) {    
  68.  
  69.                            if (xhr.status==200) {    
  70.  
  71.                                var data = eval(xhr.responseText);    
  72.  
  73.                               that.match(quickExpr,search,data); /* 相同于成功的回调函数 */    
  74.  
  75.                            } else {    
  76.  
  77.                                alert( "请求页面异常!" ); /* 请求失败 */    
  78.  
  79.                            }    
  80.  
  81.                        }    
  82.  
  83.                    };    
  84.  
  85.                    xhr.send( null );    
  86.  
  87.                },    
  88.  
  89.               fetch: function (ajax,search,quickExpr){    
  90.  
  91.                    var that= this ;    
  92.  
  93.                    this .ajax(ajax.type,ajax.url+search,quickExpr,search);    
  94.  
  95.               },    
  96.  
  97.                initEvent: function (){ /* 各事件的集合 */    
  98.  
  99.                    var that= this ;    
  100.  
  101.                   this .input.onfocus = function (){    
  102.  
  103.                        if ( this .inputValue) this .value = this .inputValue;    
  104.  
  105.                        var value= this .value, quickExpr=RegExp( '^' +value, 'i' ), self= this ;    
  106.  
  107.                        var els = that.popup.getElementsByTagName( 'a' );    
  108.  
  109.                        if (els.length>0) that.popup.style.display = 'block' ;    
  110.  
  111.                        that.timer=setInterval( function (){    
  112.  
  113.                            if (value!=self.value){ /* 判断输入内容是否改变,兼容中文 */    
  114.  
  115.                               value=self.value;    
  116.  
  117.                                that.popup.innerHTML= '' ;    
  118.  
  119.                                if (value!= '' ){    
  120.  
  121.                                   quickExpr=RegExp( '^' +value);    
  122.  
  123.                                    if (that.o.source) that.match(quickExpr,value,that.o.source);    
  124.  
  125.                                    else if (that.o.ajax) that.fetch(that.o.ajax,value,quickExpr);    
  126.  
  127.                                }    
  128.  
  129.                            }    
  130.  
  131.                        },200);    
  132.  
  133.                    };    
  134.  
  135.                   this .input.onblur = function (){ /*  输入框添加事件 */    
  136.  
  137.                        if ( this .value!= this .defaultValue) this .inputValue = this .value;    
  138.  
  139.                        clearInterval(that.timer);    
  140.  
  141.                        var current=-1; /* 记住当前有焦点的选项 */    
  142.  
  143.                        var els = that.popup.getElementsByTagName( 'a' );    
  144.  
  145.                        var len = els.length-1;    
  146.  
  147.                       var aClick = function (){    
  148.  
  149.                           that.input.inputValue = this .firstChild.nodeValue;    
  150.  
  151.                            that.popup.innerHTML= '' ;    
  152.  
  153.                            that.popup.style.display= 'none' ;    
  154.  
  155.                           that.input.focus();    
  156.  
  157.                        };    
  158.  
  159.                        var aFocus = function (){    
  160.  
  161.                            for ( var i=len; i>=0; i--){    
  162.  
  163.                                if ( this .parentNode===that.popup.children[i]){    
  164.  
  165.                                   current = i;    
  166.  
  167.                                    break ;    
  168.  
  169.                                }    
  170.  
  171.                            }    
  172.  
  173.                            //that.input.value = this.firstChild.nodeValue;    
  174.  
  175.                            for ( var k in that.o.elemCSS.focus){    
  176.  
  177.                                this .style[k] = that.o.elemCSS.focus[k];    
  178.  
  179.                            }    
  180.  
  181.                        };    
  182.  
  183.                       var aBlur= function (){    
  184.  
  185.                           for ( var k in that.o.elemCSS.blur)    
  186.  
  187.                               this .style[k] = that.o.elemCSS.blur[k];    
  188.  
  189.                        };    
  190.  
  191.                        var aKeydown = function (event){    
  192.  
  193.                            eventevent = event || window.event; /* 兼容IE */    
  194.  
  195.                            if (current === len && event.keyCode===9){ /* tab键时popup隐藏 */    
  196.  
  197.                                that.popup.style.display = 'none' ;    
  198.  
  199.                            } else if (event.keyCode==40){ /* 处理上下方向键事件方便选择提示的选项 */    
  200.  
  201.                                current++;    
  202.  
  203.                                if (current<-1) current=len;    
  204.  
  205.                               if (current>len){    
  206.  
  207.                                    current=-1;    
  208.  
  209.                                   that.input.focus();    
  210.  
  211.                                } else {    
  212.  
  213.                                    that.popup.getElementsByTagName( 'a' )[current].focus();    
  214.  
  215.                                }    
  216.  
  217.                            } else if (event.keyCode==38){    
  218.  
  219.                                current--;    
  220.  
  221.                               if (current==-1){    
  222.  
  223.                                    that.input.focus();    
  224.  
  225.                               } else if (current<-1){    
  226.  
  227.                                    current = len;    
  228.  
  229.                                    that.popup.getElementsByTagName( 'a' )[current].focus();    
  230.  
  231.                                } else {    
  232.  
  233.                                   that.popup.getElementsByTagName( 'a' )[current].focus();    
  234.  
  235.                                }    
  236.  
  237.                            }    
  238.  
  239.                        };    
  240.  
  241.                        for ( var i=0; i 
  242.                            els[i].onclick = aClick;    
  243.  
  244.                            els[i].onfocus = aFocus;    
  245.  
  246.                            els[i].onblur = aBlur;    
  247.  
  248.                            els[i].onkeydown = aKeydown;    
  249.  
  250.                        }    
  251.  
  252.                    };    
  253.  
  254.                    this .input.onkeydown = function (event){    
  255.  
  256.                       eventevent = event || window.event; /* 兼容IE */    
  257.  
  258.                        var els = that.popup.getElementsByTagName( 'a' );    
  259.  
  260.                        if (event.keyCode==40){    
  261.  
  262.                            if (els[0]) els[0].focus();    
  263.  
  264.                        } else if (event.keyCode==38){    
  265.  
  266.                           if (els[els.length-1]) els[els.length-1].focus();    
  267.  
  268.                        } else if (event.keyCode==9){    
  269.  
  270.                            if (event.shiftKey== true ) that.popup.style.display = 'none' ;    
  271.  
  272.                        }    
  273.  
  274.                    };    
  275.  
  276.                    this .e.onmouseover = function (){ that.show=1; };    
  277.  
  278.                    this .e.onmouseout = function (){ that.show=0; };    
  279.  
  280.                    addEvent.call(document, 'click' , function (){    
  281.  
  282.                        if (that.show==0){    
  283.  
  284.                            that.popup.style.display= 'none' ;    
  285.  
  286.                        }    
  287.  
  288.                    }); /* 处理提示框dom元素不支持onblur的情况 */    
  289.  
  290.                }    
  291.  
  292.            };    
  293.  
  294.            handlerhandler.prototype.init.prototype=handler.prototype; /* JQuery style,这样我们在处的时候就不用每个dom元素都用new来创建对象了 */    
  295.  
  296.            return handler; /* 把内部的处理函数传到外部 */    
  297.  
  298.        })();    
  299.  
  300.        if ( this .length){ /* 处理选择多个dom元素 */    
  301.  
  302.            for ( var a= this .length-1; a>=0; a--){ /* 调用方法为每个选择的dom生成一个处理对象,使它们不互相影响 */    
  303.  
  304.                handler( this [a],o);    
  305.  
  306.            }    
  307.  
  308.        } else { /* 处理选择一个dom元素 */    
  309.  
  310.            handler( this ,o);    
  311.  
  312.       }    
  313.  
  314.        return this ;    
  315.  
  316.   };    
  317.  
  318.   return window.autoComplete = autoComplete; /* 暴露方法给全局对象 */    
  319.  
  320.   /* 插件结束 */    
  321.  
  322.   })(window);    
  323.  

其中了一些全局的自定义函数,如addEvent和在例子中将要用到的getElementsByClassName函数如下:

view source print ?

 

  1. var getElementsByClassName = function (searchClass, node, tag) { /* 兼容各浏览器的选择class的方法;(写法参考了博客园:http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1529640.html  
  2. ,想了解更多请看这个地址) */    
  3.  
  4.       nodenode = node || document, tagtag = tag ? tag.toUpperCase() : "*" ;    
  5.  
  6.        if (document.getElementsByClassName){ /* 支持getElementsByClassName的浏览器 */    
  7.  
  8.           var temp = node.getElementsByClassName(searchClass);    
  9.  
  10.            if (tag== "*" ){    
  11.  
  12.                return temp;    
  13.  
  14.           } else {    
  15.  
  16.                var ret = new Array();    
  17.  
  18.                for ( var i=0; i 

【编辑推荐】

  1. JavaScript跨域总结与解决办法
  2. 使用Javascript开发移动应用程序
  3. 10个令人惊奇的HTML5和JavaScript效果
  4. JavaScript初学者应注意的七个细节

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...