仿百度搜索提示框jQuery自动完成

大概要实现的内容

      这是一个很简单的示例,服务器端只是用了一个jsp页面,返回的类型为xml。先讲下是怎么回事,就是在浏览器端,通过ajax请求,发送一串英文字母,服务器端通过比较,返回具有相同前缀的英文单词。就这么个意思。

      工程是在IntelliJ IDE中完成的。做前端开发感觉用IntelliJ比较方便,因为对于写javascript的话,有函数名的提示。

      本例提供下载。望各位提出宝贵意见哈。

 

一、客户端JSP页面

 

Html代码   收藏代码
  1. <%--  
  2.   Created by IntelliJ IDEA.  
  3.   User: JayChang  
  4.   Date: 2010-11-22  
  5.   Time: 8:33:11  
  6.   To change this template use File | Settings | File Templates.  
  7. --%>  
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>  
  9. <html>  
  10.   <head><title>AutoComplete-Sample</title>  
  11.      <link type="text/css" rel="stylesheet" href="./css/default.css">  
  12.      <script type="text/javascript" src="./jslib/jquery-1.4.2.js"></script>  
  13.      <script type="text/javascript" language="javascript">  
  14.          //高亮的索引  
  15.          var highLightIndex = -1;  
  16.          //超时的标识(对用户连续快速按下键盘的事件做延时处理,只对用户在500ms内最后一次请求,让其生效)  
  17.          var timeoutId;  
  18.          $(document).ready(function(){  
  19.             init();  
  20.             $('#auto_txt').bind('keyup',processKeyup);  
  21.          });  
  22.   
  23.          /**  
  24.           * 处理键盘按下后弹起的事件  
  25.           * @param event  
  26.           */  
  27.          function processKeyup(event){  
  28.                var myEvent = event || windows.event;  
  29.                var keyCode = myEvent.keyCode;  
  30.                //输入是字母,或者退格键则需要重新请求  
  31.                if((keyCode >= 65 && keyCode <= 90) || keyCode == 8){  
  32.                     //以下两行代码是为了防止用户快速输入导致频繁请求服务器  
  33.                     //这样便可以在用户500ms内快速输入的情况下,只请求服务器一次  
  34.                     //大大提高服务器性能  
  35.                     highLightIndex = -1;  
  36.                     clearTimeout(timeoutId);  
  37.                     timeoutId = setTimeout(processAjaxRequest,500);  
  38.                //处理上下键(up,down)  
  39.                }else if(keyCode == 38 || keyCode == 40){  
  40.                    processKeyUpAndDown(keyCode);  
  41.                //按下了回车键  
  42.                }else if(keyCode == 13){  
  43.                    processEnter();  
  44.                }  
  45.           }  
  46.   
  47.          /***  
  48.           * 初始化弹出框列表的位置,大小  
  49.           */  
  50.          function init(){  
  51.             $('#auto_div').hide();  
  52.             var pos = $('#auto_txt').position();  
  53.             var topPosition = pos.top+$('#auto_txt').height()+7;  
  54.             var leftPosition = pos.left;  
  55.             $('#auto_div').offset({top:topPosition,left:leftPosition});  
  56.             $('#auto_div').width($('#auto_txt').width());  
  57.             //$('#auto_div').css('position','absolute');  
  58.          }  
  59.   
  60.          /**  
  61.           * 处理Ajax请求  
  62.           * @param data  
  63.           */  
  64.          function processAjaxRequest(){  
  65.              $.ajax({  
  66.                  type:"post",       //http请求方法,默认:"post"  
  67.                  url:"data.jsp",   //发送请求的地址  
  68.                  data:"reqWord="+$('#auto_txt').val(),  
  69.                  dataType:"xml",   //预期服务器返回的数据类型  
  70.                  success:processAjaxResponse  
  71.               });  
  72.          }  
  73.   
  74.          /**  
  75.           * 处理Ajax回复  
  76.           * @param data Ajax请求得到的返回结果为dom文档对象  
  77.           */  
  78.          function processAjaxResponse(data){  
  79.              $('#auto_div').html('').show();  
  80.              var xml = $(data);  
  81.              var words = xml.find('word');  
  82.              for(var i = 0 ; i < words.length ; i ++){  
  83.                 var word_div = $('<div></div>');  
  84.                 word_div.html(words.eq(i).text());  
  85.                 word_div.hover(fnOver,fnOut);  
  86.                 word_div.click(getAutoText);  
  87.                 $('#auto_div').append(word_div);  
  88.              }  
  89.          }  
  90.   
  91.          /**  
  92.           * 处理鼠标滑过  
  93.           */  
  94.          function fnOver(){  
  95.              //alert($(this));  
  96.               changeToWhite();  
  97.               $(this).css('background-color','pink');  
  98.               //alert($(this).index());  
  99.               highLightIndex = $(this).index();  
  100.               //下面一行注释掉了,百度搜索也没这功能,就是鼠标移动时,跟着改变搜索框的内容  
  101.               //$('#auto_txt').val($('#auto_div').children().eq(highLightIndex).html());  
  102.          }  
  103.            
  104.          /**  
  105.           * 处理鼠标移除  
  106.           */  
  107.          function fnOut(){  
  108.              $(this).css('background-color','white');  
  109.          }  
  110.   
  111.          /**  
  112.           * 得到自动填充文本  
  113.           */  
  114.          function getAutoText(){  
  115.             //有高亮显示的则选中当前选中当前高亮的文本  
  116.             if(highLightIndex != -1){  
  117.                 $('#auto_txt').val($(this).html());  
  118.                 $('#auto_div').html('').hide();  
  119.             }  
  120.          }  
  121.   
  122.          /**  
  123.           * 处理按下Enter键  
  124.           * @param keyCode  
  125.           */  
  126.          function processEnter(){  
  127.              if(highLightIndex != -1){  
  128.                 $('auto_txt').val($('#auto_div').children().eq(highLightIndex).html());  
  129.                 $('#auto_div').html('').hide();  
  130.              }  
  131.          }  
  132.   
  133.          /**  
  134.           * 处理按上下键的情况  
  135.           */  
  136.          function processKeyUpAndDown(keyCode){  
  137.              var words = $('#auto_div').children();  
  138.              var num = words.length;  
  139.              if(num <= 0) return;  
  140.              changeToWhite();  
  141.              highLightIndex = ((keyCode != 38 ? num + 1:num - 1)+highLightIndex) % num;  
  142.              words.eq(highLightIndex).css('background-color','pink');  
  143.              $('#auto_txt').val(words.eq(highLightIndex).html());  
  144.          }  
  145.   
  146.          /**  
  147.           * 如果有高亮的则把高亮变白  
  148.           */  
  149.          function changeToWhite(){  
  150.              if(highLightIndex != -1){  
  151.                  $('#auto_div').children().eq(highLightIndex).css('background-color','white');  
  152.              }  
  153.          }  
  154.      </script>  
  155.   </head>  
  156.   <body>  
  157.     自动完成示例 <input type="text" id="auto_txt"><input type="button" value="提交">  
  158.                  <div style="border-width:1px;" id="auto_div"></div>  
  159.   </body>  
  160. </html>  

 二、作为服务器端的JSP,返回的是XML

       这里稍作解释,为了方便起见,在服务器端只是很简单的处理,返回有相同前缀的英文单词。没有对空格等复杂的搜索条件作处理,本例主要还是侧重于浏览器端的JavaScript。

 

Html代码   收藏代码
  1. <%--  
  2.   Created by IntelliJ IDEA.  
  3.   User: JayChang  
  4.   Date: 2010-11-22  
  5.   Time: 8:33:22  
  6.   To change this template use File | Settings | File Templates.  
  7. --%>  
  8. <%@page contentType="text/xml; charset=UTF-8"%>  
  9. <%  
  10.     String reqWord = request.getParameter("reqWord");  
  11.     System.out.println(reqWord);  
  12. %>  
  13. <words>  
  14.     <%if("absolute".startsWith(reqWord)){%>  
  15.         <word>absolute</word>  
  16.     <%}%>  
  17.     <%if("air".startsWith(reqWord)){%>  
  18.         <word>air</word>  
  19.     <%}%>  
  20.     <%if("apple".startsWith(reqWord)){%>  
  21.         <word>apple</word>  
  22.      <%}%>  
  23.     <%if("amaze".startsWith(reqWord)){%>  
  24.         <word>amaze</word>  
  25.      <%}%>  
  26.     <%if("bat".startsWith(reqWord)){%>  
  27.         <word>bat</word>  
  28.      <%}%>  
  29.     <%if("bicycle".startsWith(reqWord)){%>  
  30.         <word>bicycle</word>  
  31.      <%}%>  
  32.     <%if("bite".startsWith(reqWord)){%>  
  33.         <word>bite</word>  
  34.      <%}%>  
  35.     <%if("bottle".startsWith(reqWord)){%>  
  36.         <word>bottle</word>  
  37.      <%}%>  
  38.     <%if("cat".startsWith(reqWord)){%>  
  39.         <word>cat</word>  
  40.      <%}%>  
  41.     <%if("carry".startsWith(reqWord)){%>  
  42.         <word>carry</word>  
  43.      <%}%>  
  44.      <%if("castle".startsWith(reqWord)){%>  
  45.         <word>castle</word>  
  46.      <%}%>  
  47. </words>  

   三、CSS样式

 

Html代码   收藏代码
  1. #auto_div{  
  2.     position:absolute;  
  3.     border-width:1px;  
  4.     border:1px #808080 solid;  
  5. }   
 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值