完成有以下功能:

  • 输入字符会把以输入字符开头的提示出来。
  • 支持上下方向键选择提示选项,支持循环
  • 支持绑定一个数组提示,支持ajax传递输入框值请求数据。
  • 支持多个选择的dom元素一块绑定数据实现输入提示。各dom元素也可以单独绑定自己的数据实现输入提示,互不影响。
  • 支持中文。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>autoComplete</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <mce:style type="text/css"><!-- .autoComplete {margin:8px;position:relative;float:left;} .autoComplete input {width:200px;height:25px;margin:0;padding:0;line-height:25px;} .autoComplete ul {z-index:-12;padding:0px;margin:0px;border:1px #333 solid;width:200px;background:white;display:none;position:absolute;left:0;top:28px;*margin-left:9px;*margin-top:2px;margin-top:1px/0;} .autoComplete li {list-style:none;} .autoComplete li a {display:block;color:#000;text-decoration:none;padding:1px 0 1px 5px;_width:97%;} .autoComplete li a:hover {color:#000;background:#ccc;border:none;} --></mce:style><style type="text/css" mce_bogus="1">.autoComplete {margin:8px;position:relative;float:left;} .autoComplete input {width:200px;height:25px;margin:0;padding:0;line-height:25px;} .autoComplete ul {z-index:-12;padding:0px;margin:0px;border:1px #333 solid;width:200px;background:white;display:none;position:absolute;left:0;top:28px;*margin-left:9px;*margin-top:2px;margin-top:1px/0;} .autoComplete li {list-style:none;} .autoComplete li a {display:block;color:#000;text-decoration:none;padding:1px 0 1px 5px;_width:97%;} .autoComplete li a:hover {color:#000;background:#ccc;border:none;}</style> <mce:script type="text/javascript"><!-- var getElementsByClassName = function (searchClass, node, tag) {/* 兼容各浏览器的选择class的方法;(写法参考了博客园:http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1529640.html,想了解更多请看这个地址) */ node = node || document, tag = tag ? tag.toUpperCase() : "*"; if(document.getElementsByClassName){/* 支持getElementsByClassName的浏览器 */ var temp = node.getElementsByClassName(searchClass); if(tag=="*"){ return temp; } else { var ret = new Array(); for(var i=0; i<temp.length; i++) if(temp[i].nodeName==tag) ret.push(temp[i]); return ret; } }else{/* 不支持getElementsByClassName的浏览器 */ var classes = searchClass.split(" "), elements = (tag === "*" && node.all)? node.all : node.getElementsByTagName(tag), patterns = [], returnElements = [], current, match; var i = classes.length; while(--i >= 0) patterns.push(new RegExp("(^|//s)" + classes[i] + "(//s|$)")); var j = elements.length; while(--j >= 0){ current = elements[j], match = false; for(var k=0, kl=patterns.length; k<kl; k++){ match = patterns[k].test(current.className); if(!match) break; } if(match) returnElements.push(current); } return returnElements; } }; var addEvent=(function(){/* 用此函数添加事件防止事件覆盖 */ if(document.addEventListener){ return function(type, fn){ this.addEventListener(type, fn, false); }; }else if(document.attachEvent){ return function(type,fn){ this.attachEvent('on'+type, function () { return fn.call(this, window.event);/* 兼容IE */ }); }; } })(); ;(function(window){ /* 插件开始 */ var autoComplete=function(o){ var handler=(function(){ var handler=function(e,o){ return new handler.prototype.init(e,o); };/* 为每个选择的dom都创建一个相对应的对象,这样选择多个dom时可以很方便地使用 */ handler.prototype={ e:null, o:null, timer:null, show:0, input:null, popup:null, init:function(e,o){/* 设置初始对象 */ this.e=e, this.o=o, this.input=this.e.getElementsByTagName(this.o.input)[0], this.popup=this.e.getElementsByTagName(this.o.popup)[0], this.initEvent();/* 初始化各种事件 */ }, match:function(quickExpr,value,source){/* 生成提示 */ var li = null; for(var i in source){ if( value.length>0 && quickExpr.exec(source[i])!=null ){ li = document.createElement('li'); li.innerHTML = '<a href="javascript:;" mce_href="javascript:;">'+source[i]+'</a>'; this.popup.appendChild(li); } } if(this.popup.getElementsByTagName('a').length) this.popup.style.display='block'; else this.popup.style.display='none'; }, ajax:function(type,url,quickExpr,search){/* ajax请求远程数据 */ var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); xhr.open(type,url,true);/* 同异步在此修改 */ xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); var that=this; xhr.onreadystatechange = function(){ if(xhr.readyState==4) { if(xhr.status==200) { var data = eval(xhr.responseText); that.match(quickExpr,search,data);/* 相同于成功的回调函数 */ }else{ alert("请求页面异常!");/* 请求失败 */ } } }; xhr.send(null); }, fetch:function(ajax,search,quickExpr){ var that=this; this.ajax(ajax.type,ajax.url+search,quickExpr,search); }, initEvent:function(){/* 各事件的集合 */ var that=this; this.input.onfocus = function(){ if(this.inputValue) this.value = this.inputValue; var value=this.value, quickExpr=RegExp('^'+value,'i'), self=this; var els = that.popup.getElementsByTagName('a'); if(els.length>0) that.popup.style.display = 'block'; that.timer=setInterval(function(){ if(value!=self.value){/* 判断输入内容是否改变,兼容中文 */ value=self.value; that.popup.innerHTML=''; if(value!=''){ quickExpr=RegExp('^'+value); if(that.o.source) that.match(quickExpr,value,that.o.source); else if(that.o.ajax) that.fetch(that.o.ajax,value,quickExpr); } } },200); }; this.input.onblur = function(){/* 输入框添加事件 */ if(this.value!=this.defaultValue) this.inputValue = this.value; clearInterval(that.timer); var current=-1;/* 记住当前有焦点的选项 */ var els = that.popup.getElementsByTagName('a'); var len = els.length-1; var aClick = function(){ that.input.inputValue = this.firstChild.nodeValue; that.popup.innerHTML=''; that.popup.style.display='none'; that.input.focus(); }; var aFocus = function(){ for(var i=len; i>=0; i--){ if(this.parentNode===that.popup.children[i]){ current = i; break; } } //that.input.value = this.firstChild.nodeValue; for(var k in that.o.elemCSS.focus){ this.style[k] = that.o.elemCSS.focus[k]; } }; var aBlur= function(){ for(var k in that.o.elemCSS.blur) this.style[k] = that.o.elemCSS.blur[k]; }; var aKeydown = function(event){ event = event || window.event;/* 兼容IE */ if(current === len && event.keyCode===9){/* tab键时popup隐藏 */ that.popup.style.display = 'none'; }else if(event.keyCode==40){/* 处理上下方向键事件方便选择提示的选项 */ current++; if(current<-1) current=len; if(current>len){ current=-1; that.input.focus(); }else{ that.popup.getElementsByTagName('a')[current].focus(); } }else if(event.keyCode==38){ current--; if(current==-1){ that.input.focus(); }else if(current<-1){ current = len; that.popup.getElementsByTagName('a')[current].focus(); }else{ that.popup.getElementsByTagName('a')[current].focus(); } } }; for(var i=0; i<els.length; i++){/* 为每个选项添加事件 */ els[i].onclick = aClick; els[i].onfocus = aFocus; els[i].onblur = aBlur; els[i].onkeydown = aKeydown; } }; this.input.onkeydown = function(event){ event = event || window.event;/* 兼容IE */ var els = that.popup.getElementsByTagName('a'); if(event.keyCode==40){ if(els[0]) els[0].focus(); }else if(event.keyCode==38){ if(els[els.length-1]) els[els.length-1].focus(); }else if(event.keyCode==9){ if(event.shiftKey==true) that.popup.style.display = 'none'; } }; this.e.onmouseover = function(){ that.show=1; }; this.e.onmouseout = function(){ that.show=0; }; addEvent.call(document,'click',function(){ if(that.show==0){ that.popup.style.display='none'; } });/* 处理提示框dom元素不支持onblur的情况 */ } }; handler.prototype.init.prototype=handler.prototype;/* JQuery style,这样我们在处的时候就不用每个dom元素都用new来创建对象了 */ return handler;/* 把内部的处理函数传到外部 */ })(); if(this.length){/* 处理选择多个dom元素 */ for(var a=this.length-1; a>=0; a--){/* 调用方法为每个选择的dom生成一个处理对象,使它们不互相影响 */ handler(this[a],o); } }else{/* 处理选择一个dom元素 */ handler(this,o); } return this; }; return window.autoComplete = autoComplete;/* 暴露方法给全局对象 */ /* 插件结束 */ })(window); /* 调用 */ addEvent.call(null,'load',function(){ autoComplete.call( getElementsByClassName('autoComplete'), {/* 使用call或apply调用此方法 */ source:['0123','023',123,1234,212,214,'033333','0352342',1987,17563,20932],/* 提示时在此数组中搜索 */ //ajax:{ type:'post',url:'./php/fetch.php?search=' },/* 如果使用ajax则远程返回的数据格式要与source相同 */ elemCSS:{ focus:{'color':'black','background':'#ccc'}, blur:{'color':'black','background':'transparent'} },/* 些对象中的key要js对象中的style属性支持 */ input:'input',/* 输入框使用input元素 */ popup:'ul'/* 提示框使用ul元素 */ }); }); // --></mce:script> </head> <body><!-- 这所以使用这么多的z-index是因为ie6和ie7的问题 --> <div> <div class="autoComplete" style="z-index:19" mce_style="z-index:19"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:18" mce_style="z-index:18"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:17" mce_style="z-index:17"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:16" mce_style="z-index:16"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:15" mce_style="z-index:15"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:14" mce_style="z-index:14"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:13" mce_style="z-index:13"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:12" mce_style="z-index:12"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:11" mce_style="z-index:11"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:10" mce_style="z-index:10"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:9" mce_style="z-index:9"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:8" mce_style="z-index:8"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:7" mce_style="z-index:7"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:6" mce_style="z-index:6"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:5" mce_style="z-index:5"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:4" mce_style="z-index:4"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:3" mce_style="z-index:3"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:2" mce_style="z-index:2"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:1" mce_style="z-index:1"> <input value="input" /> <ul><li></li></ul> </div> <div class="autoComplete" style="z-index:0" mce_style="z-index:0"> <input value="input" /> <ul><li></li></ul> </div> <div style="clear:both;" mce_style="clear:both;"></div> </div> <div style="border:3px red double;margin:15px;padding:5px;" mce_style="border:3px red double;margin:15px;padding:5px;"> <h3 style="line-height:10px;" mce_style="line-height:10px;">Tip:</h3> <ul> <li>输入0、1,2会得到提示。</li> <li>用鼠标或上下键可以选择提示。</li> <li>选择点击鼠标或点回车可以选择选项。</li> <li>可以修改调用处,使各个输入框提示不同内容。</li> </ul> </div> </body> </html>

出自博客园:http://www.cnblogs.com/jaiho/archive/2011/02/28/js_autoComplete.html

用js实现输入提示(自动完成)相关推荐

  1. js验证码输入数字自动跳格

    使用Js进行验证码输入自动跳格 HTML部分: <!DOCTYPE html> <html><head><meta charset="utf-8&q ...

  2. 【高德地图API】从零开始学高德JS API(四)搜索服务——POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索

    原文地址为: [高德地图API]从零开始学高德JS API(四)搜索服务--POI搜索|自动完成|输入提示|行政区域|交叉路口|自有数据检索 摘要:地图服务,大家能想到哪些?POI搜素,输入提示,地址 ...

  3. 使用FlexBox和Json实现类似ComboBox(类似Google的输入提示和自动)功能-基于JQuery-ASP.NET...

    很久没写代码了,也很久不写技术文了,不知道该从何写起,本文将会有点乱,请见谅. 本文的内容是要实现一个类似ComboBox的功能,也可以说是类似Google的输入提示和自动完成,其实这样的文章网上也不 ...

  4. easyui select ajax,easyui的combobox根据后台数据实现自动输入提示功能

    easyui的combobox根据后台数据实现自动输入提示功能 发布时间:2020-06-11 10:09:41 来源:51CTO 阅读:1981 作者:crackernet adauhuehkek最 ...

  5. java技术:输入拼音自动提示汉字的方法

    今日课题:java项目经理与你共享"有关输入拼音自动提示汉字的方法". 一.java端     使用不同的项目获取的方法也是不一样的,但最终都要传入客户端端,这部分就不多说了,有开 ...

  6. php 智能输入提示插件,phph 输入邮箱时自动提示邮箱后缀 实现代码

    1.在html中输入邮箱的input要有自己的class,以及自己父元素的class.例如: // 初始化 $(function() { new EmailAutoComplete({ parentC ...

  7. java 输入提示_Java实现输入自动提示与补全功能

    一. 场景与目标 在使用 IDE 开发软件时, IDE 会提供一种"智能提示", 根据所输入的字符列出可能的词组: 在日常Web开发中,根据用户输入进行自动提示和补全,也能很好地改 ...

  8. PHP自动搜索框post,php搜索框提示(自动完成)实例代码_PHP教程

    百度的搜索大家都在用,当用户输入文字时,搜索框下面自动提示相关的信息,加强了用户体验,的确不错,那么这个效果是如何实现的呢 先看一下效果图吧,这样更有动力,要不然大家还不知道我在讲什么,到底要达到什么 ...

  9. vscode输入vue自动_vscode配置总结可收藏/vscode用户设置大全/vue代码模板,vscodevue...

    vscode配置总结可收藏/vscode用户设置大全/vue代码模板,vscodevue ​ ​ //用户设置 { //-------- 搜索配置 -------- "search.excl ...

最新文章

  1. Hibernate 中Datetime类型属性数据库默认值
  2. 编程指南_今晚7点,译者编程入门指南抽奖!
  3. Hibernate一对多单向关联和双向关联映射方法及其优缺点
  4. matlab中的rem和mod,matlab的rem()和mod()函数
  5. 找工作java还是python有用_你觉得学 Python 还是 Java 更好找工作?
  6. java image 设置大小_如何在Java中调整BufferedImage的大小
  7. CCIE-LAB-第八篇-SDWAN-Branch1_Branch2_Vmanage
  8. 动态规划经典题:给出两个字符串s1和s2,返回其中最大的公共子串
  9. 【精品】Deepsort文章深度解析
  10. storm-starter 例子学习
  11. 图像转svg,及绘制svg图像
  12. 子矩阵最大累加和(详解)
  13. 任一矩阵都可表为一对称矩阵和反称矩阵之和
  14. Markdown编辑器语法之代码高亮、标记和文字颜色
  15. 佳博打印机如何设置热敏打印
  16. sqlite读写锁和线程模式
  17. excel中删除重复数据
  18. 缺少微信小程序测试经验?这篇文章带你从0开始
  19. 婚姻中受伤的为什么总是女人
  20. 课堂教学评价的主要内容

热门文章

  1. Android JNI利用opengl渲染文字 (一)
  2. VS2008卸载导致的Office不能正常使用
  3. 企业应该如何做好新闻营销推广
  4. 在win7/8/10鼠标右键添加“管理员取得所有权”
  5. Python 实现海康机器人工业相机 MV-CU060-10GM 的实时显示视频流及拍照功能
  6. C#中saveFileDialog的使用
  7. Python html 爬虫 抓取论坛内容
  8. 如何让电脑永远没有弹窗广告,一招帮你解决!
  9. vector::erase()方法的详细介绍及问题解答
  10. 基于Itext的PDF国密电子签名及其实现