zoukankan      html  css  js  c++  java
  • 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);
    

      

      

  • 相关阅读:
    bzoj 4245: [ONTAK2015]OR-XOR【按位贪心】
    bzoj 4247: 挂饰【dp】
    bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】
    bzoj 3029: 守卫者的挑战【概率dp】
    bzoj 3732: Network【克鲁斯卡尔+树链剖分】
    bzoj 1040: [ZJOI2008]骑士【基环树+树形dp】
    bzoj 3668: [Noi2014]起床困难综合症【贪心】
    bzoj 2157: 旅游【树链剖分+线段树】
    bzoj 4521: [Cqoi2016]手机号码【数位dp】
    bzoj 3437: 小P的牧场【斜率优化】
  • 原文地址:https://www.cnblogs.com/realwall/p/2215485.html
Copyright © 2011-2022 走看看