<!doctype html>
<html>
<style>
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}

.auto_hidden {
    width: 204px;
    border-top: 1px solid #333;
    border-bottom: 1px solid #333;
    border-left: 1px solid #333;
    border-right: 1px solid #333;
    position: absolute;
    display: none;
}

.auto_show {
    width: 204px;
    border-top: 1px solid #333;
    border-bottom: 1px solid #333;
    border-left: 1px solid #333;
    border-right: 1px solid #333;
    position: absolute;
    z-index: 9999; /* 设置对象的层叠顺序 */
    display: block;
}

.auto_onmouseover {
    color: #ffffff;
    background-color: highlight;
    width: 100%;
}

.auto_onmouseout {
    color: #000000;
    width: 100%;
    background-color: #ffffff;
}
</style>
<script language="javascript">
<!--
    var $ = function(id) {
        return "string" == typeof id ? document.getElementById(id) : id;
    }
    var Bind = function(object, fun) {
        return function() {
            return fun.apply(object, arguments);
        }
    }
    function AutoComplete(obj, autoObj, arr) {
        this.obj = $(obj); //输入框
        this.autoObj = $(autoObj);//DIV的根节点
        this.value_arr = arr; //不要包含重复值
        this.index = -1; //当前选中的DIV的索引
        this.search_value = ""; //保存当前搜索的字符
    }
    AutoComplete.prototype = {
        //初始化DIV的位置
        init : function() {
            this.autoObj.style.left = this.obj.offsetLeft + "px";
            this.autoObj.style.top = this.obj.offsetTop + this.obj.offsetHeight
                    + "px";
            this.autoObj.style.width = this.obj.offsetWidth - 2 + "px";//减去边框的长度2px
        },
        //删除自动完成需要的所有DIV
        deleteDIV : function() {
            while (this.autoObj.hasChildNodes()) {
                this.autoObj.removeChild(this.autoObj.firstChild);
            }
            this.autoObj.className = "auto_hidden";
        },
        //设置值
        setValue : function(_this) {
            return function() {
                _this.obj.value = this.seq;
                _this.autoObj.className = "auto_hidden";
            }
        },
        //模拟鼠标移动至DIV时,DIV高亮
        autoOnmouseover : function(_this, _div_index) {
            return function() {
                _this.index = _div_index;
                var length = _this.autoObj.children.length;
                for ( var j = 0; j < length; j++) {
                    if (j != _this.index) {
                        _this.autoObj.childNodes[j].className = 'auto_onmouseout';
                    } else {
                        _this.autoObj.childNodes[j].className = 'auto_onmouseover';
                    }
                }
            }
        },
        //更改classname
        changeClassname : function(length) {
            for ( var i = 0; i < length; i++) {
                if (i != this.index) {
                    this.autoObj.childNodes[i].className = 'auto_onmouseout';
                } else {
                    this.autoObj.childNodes[i].className = 'auto_onmouseover';
                    this.obj.value = this.autoObj.childNodes[i].seq;
                }
            }
        },
        //响应键盘
        pressKey : function(event) {
            var length = this.autoObj.children.length;
            //光标键"↓"
            if (event.keyCode == 40) {
                ++this.index;
                if (this.index > length) {
                    this.index = 0;
                } else if (this.index == length) {
                    this.obj.value = this.search_value;
                }
                this.changeClassname(length);
            }
            //光标键"↑"
            else if (event.keyCode == 38) {
                this.index--;
                if (this.index < -1) {
                    this.index = length - 1;
                } else if (this.index == -1) {
                    this.obj.value = this.search_value;
                }
                this.changeClassname(length);
            }
            //回车键
            else if (event.keyCode == 13) {
                this.autoObj.className = "auto_hidden";
                this.index = -1;
            } else {
                this.index = -1;
            }
        },
        //程序入口
        start : function(event) {
            if (event.keyCode != 13 && event.keyCode != 38
                    && event.keyCode != 40) {
                this.init();
                this.deleteDIV();
                this.search_value = this.obj.value;
                var valueArr = this.value_arr;
                valueArr.sort();
                if (this.obj.value.replace(/(^\s*)|(\s*$)/g, '') == "") {
                    return;
                }//值为空,退出
                try {
                    var reg = new RegExp("(" + this.obj.value + ")", "i");
                } catch (e) {
                    return;
                }
                var div_index = 0;//记录创建的DIV的索引
                for ( var i = 0; i < valueArr.length; i++) {
                    if (reg.test(valueArr[i])) {
                        var div = document.createElement("div");
                        div.className = "auto_onmouseout";
                        div.seq = valueArr[i];
                        div.onclick = this.setValue(this);
                        div.onmouseover = this.autoOnmouseover(this, div_index);
                        div.innerHTML = valueArr[i].replace(reg,
                                "<strong>$1</strong>");//搜索到的字符粗体显示
                        this.autoObj.appendChild(div);
                        this.autoObj.className = "auto_show";
                        div_index++;
                    }
                }
            }
            this.pressKey(event);
            window.onresize = Bind(this, function() {
                this.init();
            });
        }
    }
//-->
</SCRIPT>
<body>
    <h1 align="center">自动完成函数(Autocomplete Function)</h1>
    <div align="center">
        <input type="text"
            style="width: 300px; height: 20px; font-size: 14pt;" id="o"
            onkeyup="autoComplete.start(event)">
    </div>
    <div class="auto_hidden" id="auto">
        <!--自动完成 DIV-->
    </div>
    <script>
        var autoComplete = new AutoComplete('o', 'auto', [ 'b0', 'b12', 'b22',
                'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b2', 'abd', 'ab', 'acd',
                'accd', 'b1', 'cd', 'ccd', 'cbcv', 'cxf' ]);
    </SCRIPT>
</body>
</html>

;

js自动补全的更多相关文章

  1. js自动补全空白列(即缺少td的列)

    //自动补全空白列 var rows = document.getElementById("gridTable").rows; //行对象 var allcells = rows[ ...

  2. js自动补全实例

    var oInputField ,oPopDiv , oColorsUl,aColors; //初始化变量 function initVars(modelId,divId,ulId){ oInputF ...

  3. 原创js自动补全---auotocomplete

    if ($("input.autocomplete_input").length > 0) { $("input.autocomplete_input") ...

  4. Visual Studio Code使用typings拓展自动补全功能

    转自:http://blog.csdn.net/liyijun4114/article/details/51658087 参考来源: 官方介绍: https://code.visualstudio.c ...

  5. js邮箱自动补全

    邮箱自动补全js和jQuery html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  6. jquery input 搜索自动补全、typeahead.js

    最近做个一个功能需要用到自动补全,然后在网上找了很久,踩了各种的坑 最后用typeahead.js这个插件,经过自己的测试完美实现 使用方法:在页面中引入jquery.jquery.typeahead ...

  7. jQuery搜索框自动补全功能插件实现-autocomplete.js

    最近用nodeclub实现股票的输入关键字自动补全股票信息进行搜索功能,原先用jQuery-ui,结果jQuery-ui库太大,所以考虑用其他插件,最终选择使用autocomplete.js,控件简单 ...

  8. jQuery 邮箱下拉列表自动补全

    综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...

  9. jQuery AutoComplete 自动补全

    jQuery.AutoComplete是一个基于jQuery的自动补全插件.借助于jQuery优秀的跨浏览器特性,可以兼容Chrome/IE/Firefox/Opera/Safari等多种浏览器. 特 ...

随机推荐

  1. Hadoop之Hive详解

    1.什么是Hive hive是基于hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表 并提供类sql查询功能 2.为什么要用Hive 1.直接使用hadoop所面临的问题 人员学 ...

  2. wp8使用mvvm模式简单例子

    mvvm是silverlight/wpf下的mvc升华 通过一个简单的加法计算器例子来说明mvvm是什么 在设计界面完成设计之后,显示简单的布局,如下图: 然后来比较,传统的直接方式,mvc和mvvm ...

  3. shell中set命令

    set命令作用主要是显示系统中已经存在的shell变量,以及设置shell变量的新变量值.set命令不能够定义新的shell变量.如果要定义新的变量,可以使用declare命令以变量名=值的格式进行定 ...

  4. NFS网络文件系统的配置

    NFS网络文件系统的配置 NFS网络文件系统 NFS(network file system)网络文件系统.一种使用于分散式文件协定,有SUN公司开发.功能是通过网络让不同的机器.不同的操作系统能够分 ...

  5. 2016.6.30 java.util.concurrent.ExecutionException java.lang.OutOfMemoryError

    选中ccs项目后,选择debug on server,但是运行到一半,跳出错误: java.util.concurrent.ExecutionException: java.lang.OutOfMem ...

  6. Microsoft .Net Remoting

    什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式.从微软的产品角度来看,可以说Remoting就是DCOM的一种升级,它改善了很多功能,并极好的融合到.Net平台下.Micros ...

  7. react-native 常见问题 及 解决方案

    一.报错 Warning:Navigator:isMounted is deprecated. Instead, make sure to clean up subscriptions and pen ...

  8. Oracle case when then else end的两种用法

    查询表结构 SELECT T.COLUMN_ID, T.COLUMN_NAME, (CASE WHEN (T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'RAW' ...

  9. 微信小程序登录JAVA后台

    代码地址如下:http://www.demodashi.com/demo/12736.html 登录流程时序登录流程时序 具体的登录说明查看 小程序官方API 项目的结构图: springboot项目 ...

  10. c#关于int(或其他类型)的字段在对象初始化时默认初始化问题的解决方法

    问题: c#的wcf服务接口在后台通过自定义对象接收前台参数的时候,前台参数即使不传int类型的字段值,后台也会默认初始化为0,由于很多表示状态的int字段都是从0开始的,导致查询的时候有些不想参与查 ...