仿谷歌自动补全js

本文介绍了一种仿谷歌自动补全功能的JavaScript实现方案,该方案通过AJAX技术从服务器获取数据,并能响应用户的键盘输入进行实时更新。同时支持鼠标悬停高亮显示及上下键导航等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大四那年在东软实习时,编写的一个仿谷歌自动补全的js。而且已改造通用模板,根据onfocus="init('hotKeyword1','1','<%=path %>/google/googlesearch.mvc')"几个关键key在sevice层中控制业务逻辑。


modelgoogle.js

   //全局查询采用仿谷歌技术——ardo
	var divSize = 0;//初始div长度
	var timeoutId;//设置ajax向服务器请求的延时时间

	function init(id,donum,path)
	{	
	var highlightindex = -1;//高亮节点,全局用于鼠标和键盘事件的操作
	var wordInput = $("#"+id);//获取input的id
   	var wordInputOffset = wordInput.offset();
   	$("body").append("<div id='auto'></div>");
    	$("#auto").hide().css("border","2px #2828FF solid").css("position","absolute").css("background-color","white").css("z-index","99")
      		.css("top",wordInputOffset.top + wordInput.height() + 14 + "px")
      		.css("left",wordInputOffset.left + "px").width(wordInput.width()+3);
        
    wordInput.die("keyup");
    wordInput.keyup(function(event)
    {	
        var myEvent = event || window.event;
        var keyCode = myEvent.keyCode;
        //a-z 0-9 小键盘0-9 
        if((keyCode >= 65 && keyCode <= 90) || keyCode == 8 || keyCode == 46 || (keyCode >= 48 && keyCode <= 57) || (keyCode>=96 && keyCode<=105) || keyCode == 32)
        {
   	        var wordText = wordInput.val();
   	        var autoNode = $("#auto");
   	        
   	        if(wordText != "")
   	        {
	   	        clearTimeout(timeoutId);
	   	        
	   	        timeoutId = setTimeout(function()
	   	        {	//alert(path+wordText+donum);
	   	            $.post(path,{wordText:wordText,donum:donum},function(data,textStatus)
		   	        {	
	   	            	autoNode.html("");
		   	            $.each(data,function(entryIndex,entry)
		   	            {	
		   	            	//alert(entry);
		   	                var newDivNode = $("<div style='height:20px;cursor:hand;font-size:13px;font-family:宋体;padding-top:3px;'>").attr("id",entryIndex);
		   	                newDivNode.html(entry.replace(wordText,"<b><span style='color:#FF5809'>"+wordText+"</span></b>")).appendTo(autoNode);
		   	                
		   	                newDivNode.mouseover(function(){
		   	                    
		   	                    if(highlightindex != -1)
		   	                    {
		   	                        $("#auto").children("div").eq(highlightindex).css("background-color","white");
		   	                    }
		   	                    
		   	                    highlightindex = $(this).attr("id");
		   	                    $(this).css("background-color","#D1D1D1");
		   	                    var conText = $("#auto").children("div").eq(highlightindex).text();
            					wordInput.val(conText);
		   	                });
		   	                
		   	                newDivNode.mouseout(function()
		   	                {
		   	                    $(this).css("background-color","white");
		   	                });
		   	                
		   	                newDivNode.click(function()
		   	                {
		   	                    if(highlightindex != -1)
			    	            {
			    	                var comText = $(this).text();
			    	                $("#auto").hide();
			    	                highlightindex = -1;
			    	                wordInput.val(comText);
			    	            }
		   	                });
		   	                
		   	                divSize = divSize + 1;
		   	            });
			              
			            if(divSize != 0)
			            {
		   	                autoNode.show();
		   	            }
		   	            else
		   	            {
		   	                autoNode.hide();
		   	                highlightindex = -1;
		   	            }
		   	            
		   	        },"json");
	   	        
	   	        },500);       
	   	        
   	        }
   	        else
   	        {
   	            autoNode.hide();
   	            highlightindex = -1;
   	        }
        }
        //up
        else if(keyCode == 38)
        {
            var autoNodes = $("#auto").children("div");
            
            if(highlightindex != -1)
            {
                autoNodes.eq(highlightindex).css("background-color","white");
                highlightindex = highlightindex - 1;  
                var conText = $("#auto").children("div").eq(highlightindex).text();
            	wordInput.val(conText); 	            
            }
            
            if(highlightindex == -1)
            {
                highlightindex = autoNodes.length - 1;
                var conText = $("#auto").children("div").eq(highlightindex).text();
            	wordInput.val(conText);
            }
            
            autoNodes.eq(highlightindex).css("background-color","#D1D1D1");
        }
        //down
        else if(keyCode == 40)
        {
            var autoNodes = $("#auto").children("div");
            
            if(highlightindex != -1)
            {
                autoNodes.eq(highlightindex).css("background-color","white");
            }
            
            highlightindex = highlightindex + 1;
            var conText = $("#auto").children("div").eq(highlightindex).text();
            wordInput.val(conText);
            
            if(highlightindex == autoNodes.length || highlightindex > autoNodes.length)
            {
                highlightindex = 0;
                var conText = $("#auto").children("div").eq(highlightindex).text();
            	wordInput.val(conText);
            }
            
            autoNodes.eq(highlightindex).css("background-color","#D1D1D1");
        }
        else if(keyCode == 13)
        {
            if(highlightindex != -1)
            {
                var comText = $("#auto").hide().children("div").eq(highlightindex).text();
                highlightindex = -1;
                wordInput.val(comText);
            }
        }
    });
}


fileList.jsp
<!-- 搜索框 -->
        <div align="center">
            <div style="border:solid 0px red;  vertical-align: bottom; WIDTH: 800px;padding-top: 35px;">
                
                    <input type="hidden" id="hotKeyword" name="hotKeyword" value="${seachSlsRule.ruleKey}"/>
                    <input  id="hotKeyword1" value="${seachSlsRule.ruleKey}"  style="background-color: #FFFFFF;width: 550px;    height: 33px;   line-height: 20px;  vertical-align: middle;padding: 6px 2px;    font-size: 16px;    border: 1px #7E9DB9 solid" 
                        type="text" onfocus="init('hotKeyword1','1','<%=path %>/google/googlesearch.mvc')" />
                    <input  style="width: 80px;height: 33px;line-height: 20px;color:black; background-color: #EBEBEB;" value="搜索文档" type="button"  onclick="CheckKeyWords();"/> 
                           
                    <a id="switchAdvSearch" style="cursor:hand;color:black;vertical-align: bottom;"><font style="color: white; font-size: 10px;">高级搜索</font></a>
            </div> 
        </div>


代码下载地址:http://download.youkuaiyun.com/download/ardo_pass/9733905
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值