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

本文介绍了一个仿照Google自动补全功能的实现方案,利用HTML、CSS和JavaScript结合后台处理程序(Ashx)实现输入框的文字建议功能。具体包括前端用户界面的设计、文字建议的动态展示以及与服务器端的数据交互。

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

HTML、CSS、JS部分代码

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值