• JavaScript自动提示1.1


    修改了我之前的版本,接近完美了

    (function(window){
        var autoClue = {
    		/*
    		 * getContent 获取自动提示的数据的函数,该函数发送同步请求,并返回结果集数组
    		 * txt 文本框dom对象
    		 * clue ul无序列表dom对象
    		 * txtInitValue 文本框初始化文本
    		 * txtEnterAction 用户输入完成,按enter键后触发的函数
    		 */
            clue : function(getContent, txt, clue, txtInitValue, txtEnterAction){
            	var isInit = false;
                clue.style.display = "none";
                var cluestr = clue.id;
                var createList = function(data){
                    var i, str='', len = data.length;
                    for(i = 0; i < len && i < 10; i++){
                        str += '<li id="list'
                        	+ cluestr
                            + i
                            +'">'
                            + decodeURIComponent(data.pop());
                            +'</li>';
                    }
                    return str;
                };
                if(!isInit){
                	txt.value = txtInitValue;
                }
                txt.onfocus = function(){
                	if(!isInit || txt.value == txtInitValue){
                		txt.value = "";
                	}
                };
                txt.onblur = function(){
                	if(txt.value == ''){
                		txt.value = txtInitValue;
                	}
                };
                txt.onkeyup = function(event){
                    event = event || window.event;
                    var input = encodeURIComponent(txt.value);
                    var result = new Array();
                    var i, len = input.length, resultLen;
                    if(input == null || input == ''){
                    	isInit = true;
                        return false;
                    }
                    if(event.keyCode == 40 && clue.innerHTML){
                        var listLen = clue.children.length, i, curIndex = -1;
                        for(i = 0; i < listLen; i++){
                            var tmp = clue.childNodes[i].style.backgroundColor;
                            if(tmp != "" && tmp != null && tmp != "none"){
                                curIndex = i;
                                var curList = document.getElementById("list" + cluestr + i);
                                if(navigator.appName =="Microsoft Internet Explorer"){
                                    curList.style.backgroundColor = "";
                                }else{
                                    curList.style.backgroundColor = null;
                                }
                                if(i == listLen-1){
                                    curIndex = -1;
                                }
                                break;
                            };
                        }
                        var nextList = document.getElementById("list" + cluestr + (curIndex + 1));
                        nextList.style.backgroundColor = "#D1E5FC";
                        txt.value = nextList.innerHTML;
                        txt.focus();
                        return false;
                    }
                    if(event.keyCode == 38 && clue.innerHTML){
                        var listLen = clue.children.length, i, curIndex = 0;
                        for(i = 0; i < listLen; i++){
                            var tmp = clue.childNodes[i].style.backgroundColor;
                            if(tmp != "" && tmp != null && tmp != "none"){
                                curIndex = i;
                                var curList = document.getElementById("list" + cluestr + i);
                                if(navigator.appName =="Microsoft Internet Explorer"){
                                    curList.style.backgroundColor = "";
                                }else{
                                    curList.style.backgroundColor = null;
                                }
                                if(i == listLen-1){
                                    curIndex = -1;
                                }
                                break;
                            };
                        }
                        var preList = document.getElementById("list" + cluestr + ((curIndex - 1 + listLen) % listLen));
                        preList.style.backgroundColor = "#D1E5FC";
                        txt.value = preList.innerHTML;
                        txt.focus();
                        return false;
                    	
                    }
                    if(event.keyCode == 13 && clue.innerHTML){
                    	var listLen = clue.children.length, i, curIndex = -1, submitFlag = true;
                        for(i = 0; i < listLen; i++){
                            var tmp = clue.childNodes[i].style.backgroundColor;
                            if(tmp != "" && tmp != null && tmp != "none"){
                            	submitFlag = false;
                                curIndex = i;
                                var curList = document.getElementById("list" + cluestr + i);
                                txt.value = curList.innerHTML;
                                clue.innerHTML = "";
                                clue.style.display = "none";
                                break;
                            };
                        }
                        if(submitFlag){
                        	txtEnterAction();
                        }
                        return false;
                    }
                    if(event.keyCode == 13 && clue.innerHTML == ''){
                    	txtEnterAction();
                        return false;
                    }
                    if(txt.value != ''){
                    	var content = getContent.call(this, txt.value);
                    }
                    for(i = 0; i < content.length; i++){
                        if(input == content[i].substring(0, len)){
                            result.push(content[i]);
                        }
                    }
                    resultLen = result.length;
                    resultStr = createList(result);
                    clue.innerHTML = resultStr;
                    clue.style.display = "block";
                    for(i = 0; i < resultLen; i++){
                        var list = document.getElementById("list" + cluestr + i);
                        list.onclick = function(){
                            txt.value = this.innerHTML;
                            clue.style.display = "none";
                        };
                    }
                };
                
                document.body.onclick = function(){
                	if(clue.style.display != "none"){
                		clue.style.display = "none";
                	}
                };
            }
        };
        window.autoClue = autoClue;
    })(window);
    

      

      

  • 相关阅读:
    linux sed
    低版本的 opencv库的 vs2010 打开 高版本opencv
    跨,跨,跨,我的2013半年总结
    收集用户行为
    文章17周项目2--通过基准线结合(三个数字排序(指针参数))
    Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹
    HTML_ul无序列表
    左右Cwnd::Create()功能出现afxwin1.inl line:21错误的解决方案
    rac下一个/tmp/bootstrap权限问题
    C 删除字符串1字符串2
  • 原文地址:https://www.cnblogs.com/realwall/p/2215485.html
Copyright © 2020-2023  润新知