仿google搜索提示

我是专做后台的,平时也弄一下 javascript 和 CSS ,之前在公司用 DWR 和 JS 做了一个类似 google 的搜索提示。现在把 DWR 去掉,用静态的 JSON 数据代替,为的就是脱离后台,可以静态看到效果。现在只整理了一部分,还不支持键盘事件。如果有时间我会把剩下的功能也整理出来。 

如果写得不好,大家请指教。 

在 IE6 FireFox Chrome 下都可以 

先看下 html 代码: 

Java代码   收藏代码
  1. <html>  
  2. <head>  
  3.     <meta http-equiv="Content-Type" content="text/html; charset=GBK" />  
  4.     <title>Auto Complete</title>  
  5.     <script type="text/javascript" src="autoComplete.js"></script>  
  6.     <link rel="stylesheet" type="text/css" href="autoComplete.css"></link>  
  7. </head>  
  8.   
  9. <body>  
  10.     <div>请输入a,b或其它字符看效果。</div>  
  11.     <div>  
  12.         <input name="summary" id="summary" class="text medium" οnkeyup="Auto.autoGet(this.value);" οnblur="Assist.autoClose();" autocomplete="off" /><input type="button" value="搜索">  
  13.     </div>  
  14.     <div>  
  15.         <ul id="autoUl"></ul>  
  16.     </div>  
  17. </body>  
  18. </html>  
Java代码   收藏代码
  1. /** 
  2.  * @author <a href="mailto:toozwd@gmail.com">张文弟</a> 
  3.  * 模仿google搜索的自动完成 
  4.  * 用最精简的代码实现最强大的功能 
  5.  */  
  6.   
  7. //instead document.getElementById()  
  8. var $ = function (id) {  
  9.     return "string" == typeof id ? document.getElementById(id) : id;  
  10. };  
  11.   
  12. //instead document.getElementById().value  
  13. var $F = function (id) {  
  14.     return $(id).value;  
  15. };  
  16.   
  17. var Auto = {  
  18.       
  19.     autoGet : function(value) {  
  20.         if(value == '') {  
  21.             this.closePop();  
  22.         } else {  
  23.             // get the return value list from json  
  24.             var jsonValue = getListJson(value).summary;  
  25.             this.autoCallback(jsonValue);  
  26.         }  
  27.     },  
  28.       
  29.     autoCallback : function(list) {  
  30.         //remove the old pop li  
  31.         this.closePop();  
  32.         if (list.length > 0) {  
  33.             for(var i = 0;i < list.length; i++){  
  34.                 $("autoUl").className = 'autoUl';  
  35.                 var autoLi = document.createElement("li");  
  36.                   
  37.                 // onmouseout event  
  38.                 autoLi.onmouseout = function() {  
  39.                     this.className = 'mouseOut';   
  40.                     Assist.isOnmouseover = false;  
  41.                 }  
  42.                   
  43.                 // onmouseover event  
  44.                 autoLi.onmouseover = function() {  
  45.                     this.className = 'mouseOver';  
  46.                     Assist.isOnmouseover = true;  
  47.                 }  
  48.                   
  49.                 // onclick event  
  50.                 autoLi.onclick = function() {  
  51.                     Assist.autoPush(this);  
  52.                 }  
  53.                   
  54.                 liValue = '<div class="resultName">' + list[i].name + '</div>';  
  55.                 liValue += '<div class="resultTimes">' + list[i].times + '&nbsp;结果</div>';  
  56.                 autoLi.innerHTML = liValue;  
  57.   
  58.                 $("autoUl").appendChild(autoLi);  
  59.             }  
  60.               
  61.             //pop the li  
  62.             var closeLi = document.createElement("li");  
  63.             closeLi.style.textAlign = "right";  
  64.             closeLi.innerHTML = '<a href="javascript:void(0);" οnclick="Auto.closePop();">关闭</a>';  
  65.             $("autoUl").appendChild(closeLi);  
  66.         }  
  67.     },  
  68.       
  69.     closePop : function() {  
  70.         var ul = $("autoUl").childNodes.length;  
  71.         for(var i = ul - 1; i >= 0 ; i--) {  
  72.             $("autoUl").removeChild($("autoUl").childNodes[i]);  
  73.         }  
  74.         $("autoUl").className = "borderNull";  
  75.     }  
  76. }  
  77.   
  78. var Assist = {  
  79.   
  80.     isOnmouseover : false,  
  81.       
  82.     // close the auto complete message when onclick the page of the body(input lose focus)  
  83.     autoClose : function() {  
  84.         if(this.isOnmouseover == false) {  
  85.             Auto.closePop();  
  86.         }  
  87.     },  
  88.       
  89.     autoPush : function(obj) {  
  90.         $("summary").value = obj.childNodes[0].firstChild.nodeValue;  
  91.         Auto.closePop();  
  92.         //document.forms[0].submit();  
  93.     }  
  94.   
  95. }  
  96.   
  97. // the json test data  
  98. function getListJson(name) {  
  99.     if(name == 'a') {  
  100.         var summaryJson = {"summary": [    
  101.             {"name""auto""times""13"},    
  102.             {"name""ajax""times""15"},    
  103.             {"name""abacus""times""33"}    
  104.             ]    
  105.         };  
  106.         return summaryJson;  
  107.     } else if(name == 'b') {  
  108.         var summaryJson = {"summary": [    
  109.             {"name""blue""times""18"},    
  110.             {"name""blind""times""24"},    
  111.             {"name""because""times""384"}    
  112.             ]    
  113.         };  
  114.         return summaryJson;  
  115.     } else {  
  116.         var summaryJson = {"summary": [    
  117.             {"name""didn't input""times""31"},  
  118.             {"name""the 'a' or 'b'""times""39"},  
  119.             {"name""display the ""times""44"},    
  120.             {"name""defult value""times""129"}    
  121.             ]    
  122.         };  
  123.         return summaryJson;  
  124.     }  
  125. }  


Java代码   收藏代码
  1. input {  
  2.     margin: 0;  
  3.     padding: 0;  
  4. }  
  5.   
  6. ul {  
  7.     margin: 0;  
  8.     padding: 0;  
  9.     list-style: none;  
  10.     background: #ffffff;  
  11. }  
  12.   
  13. ul li {  
  14.     line-height: 1.5em;  
  15.     margin: 0;  
  16.     padding: 0;  
  17.     width: 302px;  
  18.     background: #ffffff;  
  19.     cursor: default;  
  20.     font-size: 12px;  
  21.     height: 20px;  
  22. }  
  23.   
  24. .autoUl {  
  25.     margin-top: -2px;  
  26.     padding: 0;  
  27.     width: 302px;  
  28.     border: 1px solid #111111;  
  29.     position: absolute;  
  30.     background: #ffffff;  
  31. }  
  32.   
  33. .mouseOver {  
  34.     color: #ffffff;  
  35.     background: #6699FF;  
  36. }  
  37.   
  38. .mouseOut {  
  39.     color: #111111;  
  40.     background: #ffffff;  
  41. }  
  42.   
  43. .borderNull {  
  44.     border: none;  
  45. }  
  46.   
  47. .autoNone {  
  48.     display: none;  
  49. }  
  50.   
  51. .medium {  
  52.     width: 300px;  
  53. }  
  54.   
  55. .resultName {  
  56.     float: left;  
  57. }  
  58.   
  59. .resultTimes {  
  60.     color: #008000;  
  61.     text-align: right;  
  62. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值