用js实现输入提示功能
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章主要讲解了"用js实现输入提示功能",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"用js实现输入提示功能"吧!完成有以下功能:输入字符会把以输入
千家信息网最后更新 2025年11月08日用js实现输入提示功能
这篇文章主要讲解了"用js实现输入提示功能",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"用js实现输入提示功能"吧!
完成有以下功能:
输入字符会把以输入字符开头的提示出来。
支持上下方向键选择提示选项,支持循环
支持绑定一个数组提示,支持ajax传递输入框值请求数据。
支持多个选择的dom元素一块绑定数据实现输入提示。各dom元素也可以单独绑定自己的数据实现输入提示,互不影响。
支持中文。
首先是js的核心部分,其各部分都有较详细的说明,代码如下:
view source print ?
;( 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[xss_clean] = '' +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[xss_clean]= '' ; 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[xss_clean]= '' ; that.popup.style.display= 'none' ; that.input.focus(); }; var aFocus = function (){ for ( var i=len; i>=0; i--){ if ( this [xss_clean]===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){ eventevent = 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=0; a--){ /* 调用方法为每个选择的dom生成一个处理对象,使它们不互相影响 */ handler( this [a],o); } } else { /* 处理选择一个dom元素 */ handler( this ,o); } return this ; }; return window.autoComplete = autoComplete; /* 暴露方法给全局对象 */ /* 插件结束 */ })(window); 其中了一些全局的自定义函数,如addEvent和在例子中将要用到的getElementsByClassName函数如下:
view source print ?
var getElementsByClassName = function (searchClass, node, tag) { /* 兼容各浏览器的选择class的方法;(写法参考了博客园:http://www.cnblogs.com/rubylouvre/archive/2009/07/24/1529640.html ,想了解更多请看这个地址) */ nodenode = node || document, tagtag = 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感谢各位的阅读,以上就是"用js实现输入提示功能"的内容了,经过本文的学习后,相信大家对用js实现输入提示功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
提示
输入
选择
支持
功能
事件
对象
数据
元素
内容
函数
方法
处理
学习
上下
全局
多个
字符
插件
方向
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
白夜极光哪个服务器最晚开
欧盟5g网络安全建议
计算机网络技术基本功能
主板上市网络安全要求
网络安全单兵作战是填写题吗
cas服务器配置
英雄联盟手游服务器名字
网易服务器怎么买
数据库的常规在哪里
小米盒子网络安全性
表格数据库建模
服务器终端reboot什么意思
软件开发公司背景音乐
代理服务器功能
陆浑服务器
镇江营销软件开发
千岛湖底阿里云服务器贵州
这两年数据库工程师的行情
使用sql语句创建数据库操作
类似方正国际软件开发企业
网络安全法第76
记录网络安全行为水粉画
陵水黎族自治县服务器存储
河南电商软件开发应用
天津科技产业互联网有哪些
高品质戴尔服务器服务为先
营业执照软件开发所属行业
湖南使能互联网科技
比亚迪做软件开发怎么样
宜兴进口网络技术销售厂家