JS实现仿百度输入框自动匹配功能的示例代码


Posted in Javascript onFebruary 19, 2014

现在很多网站都有这种效果,在文本框输入一个字符,下边会出来相匹配的内容,这个代码就演示了这个功能是如何完成的,当然,这是个静态的,你可以将提示的内容从数据库读取出来,要用到AJAX那东东。

<HTML>
<HEAD>
<title>带输入匹配的文本框</title>
<style>
body,div {
font-family:verdana;
line-height:100%;
font-size:10pt;
}
input {
width:320px;
}
h1 {
text-align:center;
font-size:2.2em;
}
#divc {
border:1px solid #555;
}
.des {
width:500px;
background-color:lightyellow;
border:1px solid #555;
padding:25px;
margin-top:25px;
}
.mouseover {
color:#ffffff;
background-color:highlight;
width:100%;
cursor:default;
}
.mouseout {
color:#000;
width:100%;
background-color:#ffffff;
cursor:default;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function jsAuto(instanceName,objID)
{
this._msg = [];
this._x = null;
this._o = document.getElementById( objID );
if (!this._o) return;
this._f = null;
this._i = instanceName;
this._r = null;
this._c = 0;
this._s = false;
this._v = null;
this._o.style.visibility = "hidden";
this._o.style.position = "absolute";
this._o.style.zIndex = "9999";
this._o.style.overflow = "auto";
this._o.style.height = "50";
return this;
};

jsAuto.prototype.directionKey=function() { with (this)
{
var e = _e.keyCode ? _e.keyCode : _e.which;
var l = _o.childNodes.length;
(_c>l-1 || _c<0) ? _s=false : "";

if( e==40  &&  _s )
{
_o.childNodes[_c].className="mouseout";
(_c >= l-1) ? _c=0 : _c ++;
_o.childNodes[_c].className="mouseover";
}
if( e==38  &&  _s )
{
_o.childNodes[_c].className="mouseout";
_c--<=0 ? _c = _o.childNodes.length-1 : "";
_o.childNodes[_c].className="mouseover";
}
if( e==13 )
{
if(_o.childNodes[_c]  &&  _o.style.visibility=="visible")
{
_r.value = _x[_c];
_o.style.visibility = "hidden";
}
}
if( !_s )
{
_c = 0;
_o.childNodes[_c].className="mouseover";
_s = true;
}
}};

// mouseEvent.
jsAuto.prototype.domouseover=function(obj) { with (this)
{
_o.childNodes[_c].className = "mouseout";
_c = 0;
obj.tagName=="DIV" ? obj.className="mouseover" : obj.parentElement.className="mouseover";
}};
jsAuto.prototype.domouseout=function(obj)
{
obj.tagName=="DIV" ? obj.className="mouseout" : obj.parentElement.className="mouseout";
};
jsAuto.prototype.doclick=function(msg) { with (this)
{
if(_r)
{
_r.value = msg;
_o.style.visibility = "hidden";
}
else
{
alert("javascript autocomplete ERROR :\n\n can not get return object.");
return;
}
}};

// object method;
jsAuto.prototype.item=function(msg)
{
if( msg.indexOf(",")>0 )
{
var arrMsg=msg.split(",");
for(var i=0; i<arrMsg.length; i++)
{
arrMsg[i] ? this._msg.push(arrMsg[i]) : "";
}
}
else
{
this._msg.push(msg);
}
this._msg.sort();
};
jsAuto.prototype.append=function(msg) { with (this)
{
_i ? "" : _i = eval(_i);
_x.push(msg);
var div = document.createElement("DIV");
//bind event to object.
div.onmouseover = function(){_i.domouseover(this)};
div.onmouseout = function(){_i.domouseout(this)};
div.onclick = function(){_i.doclick(msg)};
var re  = new RegExp("(" + _v + ")","i");
div.style.lineHeight="140%";
div.className = "mouseout";
if (_v) div.innerHTML = msg.replace(re , "<strong>$1</strong>");
div.style.fontFamily = "verdana";

_o.appendChild(div);
}};
jsAuto.prototype.display=function() { with(this)
{
if(_f && _v!="")
{
_o.style.left = _r.offsetLeft;
_o.style.width = _r.offsetWidth;
_o.style.top = _r.offsetTop + _r.offsetHeight;
_o.style.visibility = "visible";
}
else
{
_o.style.visibility="hidden";
}
}};
jsAuto.prototype.handleEvent=function(fValue,fID,event) { with (this)
{
var re;
_e = event;
var e = _e.keyCode ? _e.keyCode : _e.which;
_x = [];
_f = false;
_r = document.getElementById( fID );
_v = fValue;
_i = eval(_i);
re = new RegExp("^" + fValue + "", "i");
_o.innerHTML="";

for(var i=0; i<_msg.length; i++)
{
if(re.test(_msg[i]))
{
_i.append(_msg[i]);
_f = true;
}
}

_i ? _i.display() : alert("can not get instance");

if(_f)
{
if((e==38 || e==40 || e==13))
{
_i.directionKey();
}
else
{
_c=0;
_o.childNodes[_c].className = "mouseover";
_s=true;
}
}
}};
window.onerror=new Function("return true;");
//-->
</SCRIPT>
</HEAD>

<BODY>
<div id="divc">
<!--this is the autocomplete container.-->
</div>
<div align="center">
<input onkeyup="jsAutoInstance.handleEvent(this.value,'auto',event)" id="auto">
</div>

<SCRIPT LANGUAGE="JavaScript">
<!--
var jsAutoInstance = new jsAuto("jsAutoInstance","divc");
jsAutoInstance.item("a-start,b-start,c-start,d-start,e-start,f-start,g-start,h-start,i-start,j-start,k-start,l-start,m-start,n-start,o-start,p-start,q-start,r-start,s-start,t-start,u-start,v-start,w-start,x-start,y-start,z-start,z-start,a-start,b-start,c-start,d-start,e-start,f-start,g-start,h-start,i-start,j-start,k-start,l-start,m-start,n-start,o-start,p-start,q-start,r-start,s-start,t-start,u-start,v-start,w-start,x-start,y-start,z-start,u-start,v-start,w-start,x-start,y-start,z-start,z-start,a-start,b-start,c-start,y-start,z-start,z-start,a-start,b-start,c-start,d-start,e-start,f-start,g-start,h-start,i-start,s-start,w-start,x-start,y-start,z-start,z-start,a-start,b-start,c-start,d-start,e-start,f-start,g-start,h-start,i-start,a-start,b-start,c-start,d-start,e-start,z-start,z-start");
jsAutoInstance.item("blueDestiny");
jsAutoInstance.item("BlueMiracle,Blue");
jsAutoInstance.item("angela,geniuslau");
jsAutoInstance.item("never-online");
//-->
</SCRIPT>
<center>请在输入框输入一个字母:</center>
</BODY>
</HTML>
Javascript 相关文章推荐
jquery创建div 实现代码
Apr 27 Javascript
从零开始学习jQuery (二) 万能的选择器
Oct 01 Javascript
Javascript 页面模板化很多人没有使用过的方法
Jun 05 Javascript
jQuery制作仿腾讯web qq用户体验桌面
Aug 20 Javascript
常用的JavaScript验证正则表达式汇总
Nov 26 Javascript
使用insertAfter()方法在现有元素后添加一个新元素
May 28 Javascript
vue+vux实现移动端文件上传样式
Jul 28 Javascript
微信小程使用swiper组件实现图片轮播切换显示功能【附源码下载】
Dec 12 Javascript
JS排序算法之冒泡排序,选择排序与插入排序实例分析
Dec 13 Javascript
JS/HTML5游戏常用算法之碰撞检测 像素检测算法实例详解
Dec 12 Javascript
vue跳转方式(打开新页面)及传参操作示例
Jan 26 Javascript
Vue打包部署到Nginx时,css样式不生效的解决方式
Aug 03 Javascript
js 数值转换为3位逗号分隔的示例代码
Feb 19 #Javascript
JS将数字转换成三位逗号分隔的样式(示例代码)
Feb 19 #Javascript
JS执行删除前的判断代码
Feb 18 #Javascript
JS弹出层单纯的绝对定位居中示例代码
Feb 18 #Javascript
jquery的live使用注意事项
Feb 18 #Javascript
js如何获取object类型里的键值
Feb 18 #Javascript
纯js写的分页表格数据为json串
Feb 18 #Javascript
You might like
ThinkPHP快速入门实例教程之数据分页
2014/07/01 PHP
PHP中实现获取IP和地理位置类分享
2015/02/10 PHP
php curl中gzip的压缩性能测试实例分析
2016/11/08 PHP
Yii2框架控制器、路由、Url生成操作示例
2019/05/27 PHP
在PHP中实现使用Guzzle执行POST和GET请求
2019/10/15 PHP
jQuery在IE下使用未闭合的xml代码创建元素时的Bug介绍
2012/01/10 Javascript
基于jQuery的简单九宫格实现代码
2012/08/09 Javascript
jquery实现的V字形显示效果代码
2015/10/27 Javascript
js实现楼层效果的简单实例
2016/07/15 Javascript
jQuery实现点击查看大图并以弹框的形式居中
2016/08/08 Javascript
jQuery中用on绑定事件时需注意的事项
2017/03/19 Javascript
react-router中的属性详解
2017/06/01 Javascript
javascript 中模板方法单例的实现方法
2017/10/17 Javascript
使用FormData实现上传多个文件
2018/12/04 Javascript
jQuery使用bind动态绑定事件无效的处理方法
2018/12/11 jQuery
Python入门_浅谈逻辑判断与运算符
2017/05/16 Python
基于Python和Scikit-Learn的机器学习探索
2017/10/16 Python
Python实现的简单读写csv文件操作示例
2018/07/12 Python
利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法
2019/01/08 Python
django中使用POST方法获取POST数据
2019/08/20 Python
python web框架中实现原生分页
2019/09/08 Python
Python3直接爬取图片URL并保存示例
2019/12/18 Python
为什么是 Python -m
2020/06/19 Python
python爬虫利器之requests库的用法(超全面的爬取网页案例)
2020/12/17 Python
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
2021/02/22 Python
CSS3绘制有活力的链接下划线
2016/07/14 HTML / CSS
耐克美国官网:Nike.com
2016/08/01 全球购物
学校门卫工作职责
2013/12/07 职场文书
高中地理教学反思
2014/01/29 职场文书
试用期自我鉴定范文
2014/03/20 职场文书
快递员岗位职责
2014/09/12 职场文书
四风对照检查材料范文
2014/09/27 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
2015年高校教师个人工作总结
2015/05/25 职场文书
学者《孟子》名人名言
2019/08/09 职场文书
nginx前后端同域名配置的方法实现
2021/03/31 Servers