下午做了个“自动补全”的小例子

来源:互联网 发布:语音网络系统新生注册 编辑:程序博客网 时间:2024/04/27 18:46

HTML、CSS、JS部分代码

Code:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>模仿google自动补全</title>  
  5.     <script src="../Scripts/jquery-1.4.2-vsdoc.js" type="text/javascript"></script>  
  6.     <script type="text/javascript">  
  7.         var highlightIndex = -1; //定义高亮提示节点的索引值   
  8.         var timeOutID;//定义延时时间id   
  9.         $(function () {   
  10.             var input = $("#input");   
  11.             var inputinputOffset = input.offset();   
  12.             //装载时隐藏自动补全的提示框,并设置其基本样式(宽度,定位,边框...)   
  13.             $("#auto").hide().css("border", "1px black solid").css("position", "absolute")   
  14.             .css("top", inputOffset.top + input.width + "px")   
  15.             .css("left", inputOffset.left + "px").width(input.width() + 3);   
  16.   
  17.             $("#search").click(function () {   
  18.                 alert('你输入的内容是【' + $("#input").val() + '】已经提交的服务器');   
  19.             })   
  20.             //给文本框注册键盘按下并弹起的事件   
  21.             input.keyup(function (event) {   
  22.                 // 处理文本框中键盘按下事件   
  23.                 var myEvent = event || window.event;   
  24.                 var keyCode = myEvent.keyCode; //获取按下键盘的键值   
  25.                 //输入的是英文,或退格键、del键,应该将输入的信息提交给服务器端   
  26.                 if (keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46) {   
  27.                     //1.获取文本框中内容   
  28.                     var inputinputVal = input.val();   
  29.                     //2.将输入的信息发送给服务器端   
  30.                     if (inputVal != "") {   
  31.                         //如果输入的信息不为空,则提交给服务器   
  32.                         clearTimeout(timeOutID);//对上次未完成的延时进行取消   
  33.                         //使用延时,避免频繁交互影响服务器端性能   
  34.                         timeOutID = setTimeout(function () {   
  35.                             $.post("AutoComplete.ashx", { input: inputVal }, function (data) {   
  36.                                 //将data数据转换为jQuery对象   
  37.                                 var jQueryObj = $(data);   
  38.                                 //找到服务端返回的xml数据的所有input节点   
  39.                                 var inputNodes = jQueryObj.find("input");   
  40.                                 //遍历所有的input节点,取出单词内容,并将单词内容添加到提示框中   
  41.                                 //在添加之前,先将提示框内容清除掉,避免重复   
  42.                                 $("#auto").html("");   
  43.                                 inputNodes.each(function (i) {   
  44.                                     //遍历解析XML,获取单个节点   
  45.                                     var inputContent = $(this);   
  46.                                     //新建div节点,将单词的内容添加到该节点   
  47.                                     //再将新添加的div节点拼接到提示框div中   
  48.                                     var newDivNod = $("<div>").attr("id", i); //定义新节点并给其赋id(索引)   
  49.                                     newDivNod.html(inputContent.text()).appendTo($("#auto"));   
  50.                                     newDivNod.hover(   
  51.                                     //鼠标进入   
  52.                                 function () {   
  53.                                     if (highlightIndex != -1) {   
  54.                                         //如果有高亮节点,就取消当前的高亮节点,让其背景色变白   
  55.                                         $("#auto").children("div").eq(highlightIndex).css("background-color", "white");   
  56.                                     }   
  57.                                     //让鼠标进入的那个节点高亮显示   
  58.                                     highlightIndex = $(this).attr("id"); //将当前节点的id(其实是索引),赋值给高亮节点的索引   
  59.                                     $(this).css("background-color", "red");   
  60.                                 }, //鼠标离开   
  61.                                  function () {   
  62.                                      $(this).css("background-color", "white");   
  63.                                      // highlightIndex = -1;   
  64.                                  });   
  65.                                     //点击该高亮节点,直接将该节点的文本内容提交   
  66.                                     newDivNod.click(function () {   
  67.                                         $("#input").val($(this).text());   
  68.                                         $("#auto").hide();   
  69.                                         alert('你输入的内容是【' + $(this).text() + '】已经提交的服务器');   
  70.   
  71.                                     })   
  72.                                 })   
  73.                                 //如果服务器端有返回数据,就将提示框显示   
  74.                                 if (inputNodes.length > 0) {   
  75.                                     $("#auto").show();   
  76.                                 } else {   
  77.                                     $("#auto").hide();   
  78.                                 }   
  79.                             }, "xml");   
  80.                         }, 500)   
  81.   
  82.                     } else {   
  83.                         $("#auto").hide(); //如果输入框的内容为空,隐藏提示框   
  84.                     }   
  85.                 } else if (keyCode == 38 || keyCode == 40) {   
  86.                     //如果按的是向上键38或向下键40   
  87.                     if (keyCode == 38) {   
  88.                         //向上   
  89.                         var autoNodes = $("#auto").children("div"); //获取提示框中的全部提示内容--div子节点   
  90.                         if (highlightIndex != -1) {   
  91.                             //如果原来存在高亮节点,则将背景色改为白色   
  92.                             autoNodes.eq(highlightIndex).css("background-color", "white");   
  93.                             highlightIndex--; //索引值自减   
  94.                         } else {   
  95.                             highlightIndex = autoNodes.length - 1;   
  96.                         }   
  97.   
  98.                         if (highlightIndex == -1) {   
  99.                             //如果索引值为-1后,将索引值指回最后一个元素节点,让最后一个元素高亮   
  100.                             highlightIndex = autoNodes.length - 1;   
  101.                         };   
  102.                         //让现在高亮的节点背景色变成红色   
  103.                         autoNodes.eq(highlightIndex).css("background-color", "red");   
  104.                         //获取高亮节点的文本内容,并赋给文本框   
  105.                         var txtVal = $("#auto").children("div").eq(highlightIndex).text();   
  106.                         $("#input").val(txtVal);   
  107.                     }   
  108.                     else {   
  109.                         //向下   
  110.                         var autoNodes = $("#auto").children("div");   
  111.                         if (highlightIndex != -1) {   
  112.                             autoNodes.eq(highlightIndex).css("background-color", "white");   
  113.                         }   
  114.                         highlightIndex++;   
  115.                         if (highlightIndex == autoNodes.length) {   
  116.                             highlightIndex = 0;   
  117.                         }   
  118.                         autoNodes.eq(highlightIndex).css("background-color", "red");   
  119.                         var txtVal = $("#auto").children("div").eq(highlightIndex).text();   
  120.                         $("#input").val(txtVal);   
  121.                     }   
  122.                 } else if (keyCode == 13) {   
  123.                     //如果按下的是回车   
  124.                     alert('你输入的内容是【' + $("#input").val() + '】已经提交的服务器');   
  125.                     $("#auto").hide(); //让提示框隐藏   
  126.                     //让文本框失去焦点(避免循环弹框)   
  127.                     $("#input").get(0).blur();   
  128.                 }   
  129.             })   
  130.   
  131.         })   
  132.     </script>  
  133. </head>  
  134. <body>  
  135. <center>  
  136.  <input type="text"  id="input"/>  
  137. <input type="button" value="想知道,就丁浪一下" id="search" /><br />  
  138. </center>  
  139. <div id="auto"></div>  
  140.   
  141. </body>  
  142. </html>  

C#部分代码:ashx一般处理程序

Code:
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5.   
  6. namespace LearningAspNet.自动补全   
  7. {   
  8.     /// <summary>   
  9.     /// AutoComplete 的摘要说明   
  10.     /// </summary>   
  11.     public class AutoComplete : IHttpHandler   
  12.     {   
  13.   
  14.         public void ProcessRequest(HttpContext context)   
  15.         {   
  16.             context.Response.ContentType = "text/xml";//更改MIME类型,因为要返回xml数据   
  17.             //接收客户端输入的数据,用于和服务器端的“单词”匹配(初步模型)   
  18.   
  19.           string input = context.Request["input"];   
  20.       
  21.   
  22.             //匹配并输出返回XML格式的数据   
  23.           context.Response.Write("<inputs>");//XML根节点   
  24.           if ("abs".StartsWith(input))   
  25.             {   
  26.                 context.Response.Write("<input>abs</input>");   
  27.             }   
  28.           if ("add".StartsWith(input))   
  29.             {   
  30.                 context.Response.Write("<input>add</input>");   
  31.             }   
  32.           if ("auto".StartsWith(input))   
  33.             {   
  34.                 context.Response.Write("<input>auto</input>");   
  35.             }   
  36.           if ("apach".StartsWith(input))   
  37.             {   
  38.                 context.Response.Write("<input>apach</input>");   
  39.             }   
  40.           if ("break".StartsWith(input))   
  41.             {   
  42.                 context.Response.Write("<input>break</input>");   
  43.             }   
  44.           if ("blush".StartsWith(input))   
  45.             {   
  46.                 context.Response.Write("<input>blush</input>");   
  47.             }   
  48.           if ("bool".StartsWith(input))   
  49.             {   
  50.                 context.Response.Write("<input>bool</input>");   
  51.             }   
  52.           if ("blue".StartsWith(input))   
  53.             {   
  54.                 context.Response.Write("<input>blue</input>");   
  55.             }   
  56.           if ("bind".StartsWith(input))   
  57.             {   
  58.                 context.Response.Write("<input>bind</input>");   
  59.             }   
  60.           if ("context".StartsWith(input))   
  61.             {   
  62.                 context.Response.Write("<input>context</input>");   
  63.             }   
  64.             if ("content".StartsWith(input))   
  65.             {   
  66.                 context.Response.Write("<input>content</input>");   
  67.             }   
  68.             if ("campaq".StartsWith(input))   
  69.             {   
  70.                 context.Response.Write("<input>campaq</input>");   
  71.             }   
  72.             if ("cool".StartsWith(input))   
  73.             {   
  74.                 context.Response.Write("<input>cool</input>");   
  75.             }   
  76.             if ("code".StartsWith(input))   
  77.             {   
  78.                 context.Response.Write("<input>code</input>");   
  79.             }   
  80.   
  81.             if ("case".StartsWith(input))   
  82.             {   
  83.                    
  84.                 context.Response.Write("<input>case</input>");   
  85.                       
  86.             }   
  87.             if ("drect".StartsWith(input))    
  88.             {   
  89.                 context.Response.Write("<input>drect</input>");   
  90.             }   
  91.             if ("deafault".StartsWith(input))    
  92.             {   
  93.                 context.Response.Write("<input>deafault</input>");   
  94.             }   
  95.             context.Response.Write("</inputs>");//xml根节点   
  96.         }   
  97.   
  98.         public bool IsReusable   
  99.         {   
  100.             get  
  101.             {   
  102.                 return false;   
  103.             }   
  104.         }   
  105.     }   
  106. }  

 

原创粉丝点击