pasckr 发表于 2016-5-9 15:29:48

DedeCMS 使用Ajax 实现搜索下拉提示




                这个功能主要实现的是用户在搜索的时候输入关键字,然后会出现下拉菜单,下拉菜单中出现的是站内所有包含这个关键字的文章的标题共用户选择,相对DedeCMS原生的搜索这个功能实用一点,既增加了用户体验,也可以让用户快速的找到想要的东西。功能基于php+jquery来实现,参考autocomplete,效果图如下:
http://img10.3lian.com/edu120730/g/g106/201208/a1e4100ed962028c7f4c9f6ad84886d9.png
  下面讲解下具体实现步骤:
1、打开你的网站首页模板,在</head>之前加入
<script language=&quot;javascript&quot; type=&quot;text/javascript&quot; src=&quot;{dede:global.cfg_templets_skin/}/js/jquery-1.7.1.min.js&quot;></script><script type=&quot;text/javascript&quot;>   function lookup(inputString) {         if(inputString.length == 0) {             // Hide the suggestion box.             $('#suggestions').hide();         } else {             $.post(&quot;/plus/search_list.php&quot;, {queryString: &quot;&quot;+inputString+&quot;&quot;}, function(data){               if(data.length >0) {                     $('#suggestions').show();                     $('#autoSuggestionsList').html(data);               }             });         }   } // lookup          function fill(thisValue) {         $('#inputString').val(thisValue);         setTimeout(&quot;$('#suggestions').hide();&quot;, 200);   } </script>   jquery-1.7.1.min.js这个jquery库需要你自己下载,这里就不多说了。本人用的是这个版本的。
  这段代码中的search_list.php就是本文下载的文件,下载后放入/plus目录下。
2、打开head.htm,找到搜索部分的from表单代码修改为
<formname=&quot;formsearch&quot; action=&quot;{dede:global.cfg_cmsurl/}/plus/search.php&quot; id=&quot;formkeyword&quot;>         <label for=&quot;header-subscribe-email&quot; class=&quot;text&quot;> </label>         <input type=&quot;hidden&quot; name=&quot;kwtype&quot; value=&quot;0&quot; />         <input type=&quot;text&quot; name=&quot;q&quot; size=&quot;24&quot;value=&quot;在这里搜索...&quot; onfocus=&quot;if(this.value=='在这里搜索...'){this.value='';}&quot;onblur=&quot;if(this.value==''){this.value='在这里搜索...';}&quot; id=&quot;inputString&quot; onkeyup=&quot;lookup(this.value);&quot; onblur=&quot;fill();&quot; class=&quot;f-text&quot;>         <input type=&quot;submit&quot; class=&quot;commit&quot; value=&quot;搜索&quot; />         <div class=&quot;suggestionsBox&quot; id=&quot;suggestions&quot; style=&quot;display: none;&quot;>             <div class=&quot;suggestionList&quot;><ul id=&quot;autoSuggestionsList&quot;></ul></div>         </div>         </form>   这部分可以根据你自己的代码的实际情况具体修改,主要是输入关键字的input和下边加的DIV层。
3、打开你自己的样式表css文件,在最后加入
.suggestionsBox { position:relative; left:0px;width: 250px; background: white;border: 1px solid #dcdcdc;color: #323232; z-index:999; } .suggestionList { margin: 0px; padding: 0px; } .suggestionList li { margin: 0px 0px 3px 0px; position:relative;padding: 3px; cursor: pointer;list-style:none;padding-left:5px;height:20px;overflow:hidden} .suggestionList li:hover { background-color: #659CD8; } .jr{position:absolute;top:9px;right:-5px}   此样式可以根据自己的网站定义噢。
  到这添加的代码就完了,然后下载search_list.rar,下载后解压得search_list.php到放入/plus目录下。在这个文件里有详细注解。可根据实际情况调整。所有结果都是由这个文件来返回的。最后去前台试试您的效果把。
  本文由似水星辰原创,本文原地址:http://www.sitejs.com/sitejs-12281-1.html
页: [1]
查看完整版本: DedeCMS 使用Ajax 实现搜索下拉提示