Javascript-based automatic matching of input boxes similar to Baidu and javascript

Source: Internet
Author: User

Javascript-based automatic matching of input boxes similar to Baidu and javascript

This example explains how to use javascript to implement the auto-match function of the Baidu input box. Many websites now have this effect. If you enter a character in the text box, the matching content will be 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 content 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: defaul T ;}. 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. direkey key = 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" contains too many jsauto.prototype.doc lick = function (msg) {with (this) {if (_ r) {_ r. value = msg; _ o. style. visibility = "hidden";} else {alert ("javascript autocomplete ERROR: \ 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 .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>

I hope this article will help you learn about javascript programming.

Articles you may be interested in:
  • Js obtains the Chinese pinyin, and Select automatically matches the code of the letter to get the value
  • Example of automatically matching characters in the input box using js
  • How to Use JSuggest to automatically match the drop-down list (sample code)
  • Sample Code for JS to implement auto-match function like Baidu input box

Related Article

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.