javascript 邮箱输入自动补全插件(转)

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>inputSuggest_0.1.js</title>
        <style type="text/css">
            body{margin:0;padding:0;}
            input{width:200px;}
            .suggest-container{border:1px solid #C1C1C1;visibility:hidden;}
            .suggest-item{padding:3px 2px;}
            .suggest-active {background:#33CCFF;color:white;padding:3px 2px;}
        </style>
        <script type="text/javascript" >
        /**
 * JavaScript inputSuggest v0.1
 * Copyright (c) 2010 snandy
 * Blog: http://snandy.javaeye.com/
 * QQ群: 34580561
 * Date: 2010-09-25
 * Download by http://www.codefans.net
 *
 * new InputSuggest({
 *       input         HTMLInputElement 必选
 *       data             Array [‘sina.cn‘,‘sina.com‘,‘2008.sina.com‘,‘vip.sina.com.cn‘] 必选
 *       containerCls    容器className
 *       itemCls        容器子项className
 *       activeCls        高亮子项className
 *       width         宽度设置 此项将覆盖containerCls的width
 *    opacity        透明度设置 此项将覆盖containerCls的opacity
 * });
 */

function InputSuggest(opt){
    this.win = null;
    this.doc = null;
    this.container = null;
    this.items = null;
    this.input = opt.input || null;
    this.containerCls = opt.containerCls || ‘suggest-container‘;
    this.itemCls = opt.itemCls || ‘suggest-item‘;
    this.activeCls = opt.activeCls || ‘suggest-active‘;
    this.width = opt.width;
    this.opacity = opt.opacity;
    this.data = opt.data || [];
    this.active = null;
    this.visible = false;
    this.init();
}
InputSuggest.prototype = {
    init: function(){
        this.win = window;
        this.doc = window.document;
        this.container = this.$C(‘div‘);
        this.attr(this.container, ‘class‘, this.containerCls);
        this.doc.body.appendChild(this.container);
        this.setPos();
        var _this = this, input = this.input;        

        this.on(input,‘keyup‘,function(e){
            if(input.value==‘‘){
                _this.hide();
            }else{
                _this.onKeyup(e);
            }

        });
        // blur会在click前发生,这里使用mousedown
        this.on(input,‘blur‘,function(e){
            _this.hide();
        });
        this.onMouseover();
        this.onMousedown();

    },
    $C: function(tag){
        return this.doc.createElement(tag);
    },
    getPos: function (el){
        var pos=[0,0], a=el;
        if(el.getBoundingClientRect){
            var box = el.getBoundingClientRect();
            pos=[box.left,box.top];
        }else{
            while(a && a.offsetParent){
                pos[0] += a.offsetLeft;
                pos[1] += a.offsetTop;
                a = a.offsetParent
            }
        }
        return pos;
    },
    setPos: function(){
        var input = this.input,
            pos = this.getPos(input),
            brow = this.brow,
            width = this.width,
            opacity = this.opacity,
            container = this.container;
        container.style.cssText =
            ‘position:absolute;overflow:hidden;left:‘
            + pos[0] + ‘px;top:‘
            + (pos[1]+input.offsetHeight) + ‘px;width:‘
            // IE6/7/8/9/Chrome/Safari input[type=text] border默认为2,Firefox为1,因此取offsetWidth-2保证与FF一致
            + (brow.firefox ? input.clientWidth : input.offsetWidth-2) + ‘px;‘;
        if(width){
            container.style.width = width + ‘px‘;
        }
        if(opacity){
            if(this.brow.ie){
                container.style.filter = ‘Alpha(Opacity=‘ + opacity * 100 + ‘);‘;
            }else{
                container.style.opacity = (opacity == 1 ? ‘‘ : ‘‘ + opacity);
            }
        }
    },
    show: function(){
        this.container.style.visibility = ‘visible‘;
        this.visible = true;
    },
    hide: function(){
        this.container.style.visibility = ‘hidden‘;
        this.visible = false;
    },
    attr: function(el, name, val){
        if(val === undefined){
            return el.getAttribute(name);
        }else{
            el.setAttribute(name,val);
            name==‘class‘ && (el.className = val);
        }
    },
    on: function(el, type, fn){
        el.addEventListener ? el.addEventListener(type, fn, false) : el.attachEvent(‘on‘ + type, fn);
    },
    un: function(el, type, fn){
        el.removeEventListener ? el.removeEventListener(type, fn, false) : el.detachEvent(‘on‘ + type, fn);
    },
    brow: function(ua){
        return {
            ie: /msie/.test(ua) && !/opera/.test(ua),
            opera: /opera/.test(ua),
            firefox: /firefox/.test(ua)
        };
    }(navigator.userAgent.toLowerCase()),
    onKeyup: function(e){
        var container = this.container, input = this.input, iCls = this.itemCls, aCls = this.activeCls;
        if(this.visible){
            switch(e.keyCode){
                case 13: // Enter
                    if(this.active){
                        input.value = this.active.firstChild.data;
                        this.hide();
                    }
                    return;
                case 38: // 方向键上
                    if(this.active==null){
                        this.active = container.lastChild;
                        this.attr(this.active, ‘class‘, aCls);
                        input.value = this.active.firstChild.data;
                    }else{
                        if(this.active.previousSibling!=null){
                            this.attr(this.active, ‘class‘, iCls);
                            this.active = this.active.previousSibling;
                            this.attr(this.active, ‘class‘, aCls);
                            input.value = this.active.firstChild.data;
                        }else{
                            this.attr(this.active, ‘class‘, iCls);
                            this.active = null;
                            input.focus();
                            input.value = input.getAttribute("curr_val");
                        }
                    }
                    return;
                case 40: // 方向键下
                    if(this.active==null){
                        this.active = container.firstChild;
                        this.attr(this.active, ‘class‘, aCls);
                        input.value = this.active.firstChild.data;
                    }else{
                        if(this.active.nextSibling!=null){
                            this.attr(this.active, ‘class‘, iCls);
                            this.active = this.active.nextSibling;
                            this.attr(this.active, ‘class‘, aCls);
                            input.value = this.active.firstChild.data;
                           }else{
                            this.attr(this.active, ‘class‘, iCls);
                            this.active = null;
                            input.focus();
                            input.value = input.getAttribute("curr_val");
                        }
                    }
                    return;

            }

        }
        if(e.keyCode==27){ // ESC键
            this.hide();
            input.value = this.attr(input,‘curr_val‘);
            return;
        }
        if(input.value.indexOf(‘@‘)!=-1){return;}
        this.items = [];

        if(this.attr(input,‘curr_val‘)!=input.value){
            this.container.innerHTML = ‘‘;
            for(var i=0,len=this.data.length;i<len;i++){
                var item = this.$C(‘div‘);
                this.attr(item, ‘class‘, this.itemCls);
                item.innerHTML = input.value + ‘@‘ + this.data[i];
                this.items[i] = item;
                this.container.appendChild(item);
            }
            this.attr(input,‘curr_val‘,input.value);
        }

        this.show();

    },
    onMouseover: function(){
        var _this = this, icls = this.itemCls, acls = this.activeCls;
        this.on(this.container,‘mouseover‘,function(e){
            var target = e.target || e.srcElement;
            if(target.className == icls){
                if(_this.active){
                    _this.active.className = icls;
                }
                target.className = acls;
                _this.active = target;

            }
        });
    },
    onMousedown: function(){
        var _this = this;
        this.on(this.container,‘mousedown‘,function(e){
            var target = e.target || e.srcElement;
            _this.input.value = target.innerHTML;
            _this.hide();
        });
    }
}    

        </script>

        <script type="text/javascript">

            window.onload = function(){
                var sinaSuggest = new InputSuggest({
                    input: document.getElementById(‘sina‘),
                    data: [‘sina.cn‘,‘sina.com‘,‘vip.sina.com.cn‘,‘2008.sina.com‘,‘263.sina.com‘]
                });
                var sohuSuggest = new InputSuggest({
                    width: 300,
                    opacity: 0.3,
                    input: document.getElementById(‘sohu‘),
                    data: [‘sohu.com‘,‘sogou.com‘,‘chinaren.com‘,‘vip.sohu.com‘,‘sohu.net‘,‘2008.sohu.com‘,‘sms.sohu.com‘]
                });

            }
        </script>
    </head>
    <body>
        <div style="width:400px;margin:30px auto;text-align:center;">
            <label>新浪</label>
            <input type="text" id="sina"/>
            <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
            <label>搜狐</label>
            <input type="text" id="sohu"/>
        </div>

    </body>
</html>

javascript 邮箱输入自动补全插件(转)

时间: 2024-10-29 04:35:40

javascript 邮箱输入自动补全插件(转)的相关文章

非常好用的JQuery自动补全插件bigautocomplete

自动补全插件,有些功能有限,有些是老外做的,不支持中文.今天发现一个国人做的,叫做bigautocomplete,还不错. 官网下载地址:http://code.google.com/p/jquery-bigui/downloads/list 如果不想翻墙的话,可以从这里下载(已经转码为UTF-8):http://download.csdn.net/detail/clementad/8917219 使用步骤: 1.把两个js和css文件放到项目中: 2.在项目页面中引用: <script src

Vim自动补全插件----YouCompleteMe安装与配置

Vim自动补全插件----YouCompleteMe安装与配置 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assist X这么方便快捷的补全插件呢?以前用的是neocomplcache+code_complete+omnicppcomplete,但效果还是不是很好.看到有YCM后前面的插件可以说拜拜了. YouCompleteMe:一个随键而全的.支持模糊搜索的.高速补全的插件.YCM 由 google 公司搜索项目组的软件工程师 Strahinja

【转】Vim自动补全插件----YouCompleteMe安装与配置

原文网址:http://www.cnblogs.com/zhongcq/p/3630047.html 使用Vim编写程序少不了使用自动补全插件,在Linux下有没有类似VS中的Visual Assist X这么方便快捷的补全插件呢?以前用的是neocomplcache+code_complete+omnicppcomplete,但效果还是不是很好.看到有YCM后前面的插件可以说拜拜了. YouCompleteMe:一个随键而全的.支持模糊搜索的.高速补全的插件.YCM 由 google 公司搜索

Archlinux YouCompleteMe vim自动补全插件

Archlinux 安装和配置vim补全插件YouCompleteMe的过程. 参考: https://github.com/Valloric/YouCompleteMe https://github.com/gmarik/Vundle.vim http://www.cnblogs.com/zhongcq/p/3630047.html 1.用vundle下载YouCompleteMe和syntastic 2.pacman -S clang (编译会得到/usr/lib/libclang.so)

jquery 自动补全插件源码

/* 自动补全插件 */!function($){ $.fn.extend({ autocomplete:function(arr,max){ return new $.Autocomplete(this,arr,max);//如果有,就首先生成一个 } }); //构造函数 $.Autocomplete = function(input,arr,max){ this.option = {}; this.option.data = arr;//数据 this.option.max = max;

Ubuntu16.04安装YouCompleteMe自动补全插件

最近把VMware Workstation搞坏了,里面的虚拟机都开不起来了.重装之后过了几天还是老样子,索性安装了VirtualBox来玩. 第一件事就是安装VIM的插件,搞这个东西可谓是累死,尤其是安装YouCompleteMe,所以将过程记录下来,在此分享. 一  安装VIM8.0 首先,Ubuntu16.04上直接安装vim 1 sudo apt-get install vim 此时安装的vim默认是7.4版本的,输入以下指令可升级(Ubuntu才可以): 1 sudo add-apt-r

(转) python vim编辑器自动补全插件安装

自动补全插件:pydiction 可以实现下面的python代码自动补全: 1.简单python关键词补全 2.python函数补全带括号 3.python模块补全 4.python模块内置函数,变量补全 5.form module import sub-module 补全 安装插件 wget https://github.com/rkulla/pydiction/archive/master.zip unzip -q master mv pydiction-master pydiction m

VIM自动补全插件 - YouCompleteMe--&quot;大神级vim补全插件&quot;

VIM自动补全插件 - YouCompleteMe 序言 vim 之所以被称为编辑器之神多半归功于其丰富的可DIY的灵活插件功能,( 例如vim下的这款神级般的代码补全插件YouCompleteMe), 使用VIM的用户也多半感觉 IDE开发工具不够轻便,且IDE往往只支持特定的几种语言或者特定系统平台,多数人用来用去,还是觉得VIM最顺手,用起来最舒心. 下面这个VIM的自动补全插件——YouCompleteMe,为我们弥补了vim不能自动补全的缺憾.YCM不仅仅对C族语言的支持非常好,而且它

JS实现在输入框内输入@时,邮箱账号自动补全

<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"/> <title>邮箱自动补全</title> <style type="text/css"> .wrap{width:200px;margin:0 auto;} h1{font-size:36px;text-align:center;lin