Sample Code for JS to implement auto-match function like Baidu input box
This article mainly introduces the sample code for JS to implement the auto-match function of the Baidu input box. If you need it, you can refer to it for help.
Many websites now have this effect. If you enter a character in the text box and the matched content is displayed below, this Code demonstrates how this function is completed. Of course, this is static. You can read the prompt content from the database and use AJAX.
The 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.doc lick = function (msg) {with (this)
{
If (_ r)
{
_ R. value = msg;
_ O. style. visibility = "hidden ";
}
Else
{
Alert ("javascript autocomplete ERROR: nn 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 .doc lick (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> enter a letter in the input box: </center>
</BODY>
</HTML>