JS implementation of the Baidu input box automatic matching function example code

Source: Internet
Author: User

JS to realize The imitation Baidu input box automatic matching function example code font: [Increase decrease] type: Reprint

This article is mainly for the JS implementation of Baidu input box automatic matching function of the sample code is introduced, the need for friends can come to the reference, I hope to help you

Now many sites have this effect, in the text box to enter a character, the bottom will come out of the matching content, this code demonstrates how this function is done, of course, this is a static, you can read the contents of the hint from the database, to use the Ajax stuff.

Copy CodeThe code is as follows:
<HTML>
<HEAD>
<title> text box with input matching </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 \ nthe 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> Please enter a letter in the input box:</center>
</BODY>
</HTML>

JS implementation of the Baidu input box automatic matching function example code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.