DedeCMS

这个功能主要实现的是用户在搜索的时候输入关键字,然后会出现下拉菜单,下拉菜单中出现的是站内所有包含这个关键字的文章的标题共用户选择,相对Dedecms原生的搜索这个功能实用一点,既增加了用户体验,也可以让用户快速的找到想要的东西。功能基于php+jquery来实现,参考autocomplete,效果图如下:



 下面讲解下具体实现步骤:

1、打开你的网站首页模板,在</head>之前加入

<span class="sh_keyword" style="color: rgb(165, 42, 42);"><script</span> <span class="sh_type" style="color: rgb(0, 0, 255);">language</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"javascript"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">type</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"text/javascript"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">src</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"{dede:global.cfg_templets_skin/}/js/jquery-1.7.1.min.js"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></script></span>
<span class="sh_keyword" style="color: rgb(165, 42, 42);"><script</span> <span class="sh_type" style="color: rgb(0, 0, 255);">type</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"text/javascript"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></span> 
    function lookup(inputString) { 
        if(inputString.length == 0) { 
            // Hide the suggestion box. 
            $('#suggestions').hide(); 
        } else { 
            $.post("/plus/search_list.php", {queryString: ""+inputString+""}, function(data){ 
                if(data.length >0) { 
                    $('#suggestions').show(); 
                    $('#autoSuggestionsList').html(data); 
                } 
            }); 
        } 
    } // lookup 
     
    function fill(thisValue) { 
        $('#inputString').val(thisValue); 
        setTimeout("$('#suggestions').hide();", 200); 
    } 
<span class="sh_keyword" style="color: rgb(165, 42, 42);"></script></span> 

  jquery-1.7.1.min.js这个jquery库需要你自己下载,这里就不多说了。

  这段代码中的search_list.php就是本文下载的文件,下载后放入/plus目录下。

2、打开head.htm,找到搜索部分的from表单代码修改为

<span class="sh_keyword" style="color: rgb(165, 42, 42);"><form</span>  <span class="sh_type" style="color: rgb(0, 0, 255);">name</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"formsearch"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">action</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"{dede:global.cfg_cmsurl/}/plus/search.php"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">id</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"formkeyword"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"><label</span> <span class="sh_type" style="color: rgb(0, 0, 255);">for</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"header-subscribe-email"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">class</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"text"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></span> <span class="sh_keyword" style="color: rgb(165, 42, 42);"></label></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"><input</span> <span class="sh_type" style="color: rgb(0, 0, 255);">type</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"hidden"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">name</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"kwtype"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">value</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"0"</span> <span class="sh_keyword" style="color: rgb(165, 42, 42);">/></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"><input</span> <span class="sh_type" style="color: rgb(0, 0, 255);">type</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"text"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">name</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"q"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">size</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"24"</span>  <span class="sh_type" style="color: rgb(0, 0, 255);">value</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"在这里搜索..."</span> <span class="sh_type" style="color: rgb(0, 0, 255);">onfocus</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"if(this.value=='在这里搜索...'){this.value='';}"</span>  <span class="sh_type" style="color: rgb(0, 0, 255);">onblur</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"if(this.value==''){this.value='在这里搜索...';}"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">id</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"inputString"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">onkeyup</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"lookup(this.value);"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">onblur</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"fill();"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">class</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"f-text"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"><input</span> <span class="sh_type" style="color: rgb(0, 0, 255);">type</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"submit"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">class</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"commit"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">value</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"搜索"</span> <span class="sh_keyword" style="color: rgb(165, 42, 42);">/></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"><div</span> <span class="sh_type" style="color: rgb(0, 0, 255);">class</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"suggestionsBox"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">id</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"suggestions"</span> <span class="sh_type" style="color: rgb(0, 0, 255);">style</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"display: none;"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></span> 
            <span class="sh_keyword" style="color: rgb(165, 42, 42);"><div</span> <span class="sh_type" style="color: rgb(0, 0, 255);">class</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"suggestionList"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">><ul</span> <span class="sh_type" style="color: rgb(0, 0, 255);">id</span><span class="sh_symbol">=</span><span class="sh_string" style="color: rgb(255, 0, 255);">"autoSuggestionsList"</span><span class="sh_keyword" style="color: rgb(165, 42, 42);">></ul></div></span> 
          <span class="sh_keyword" style="color: rgb(165, 42, 42);"></div></span> 
        <span class="sh_keyword" style="color: rgb(165, 42, 42);"></form></span> 

  这部分可以根据你自己的代码的实际情况具体修改,主要是输入关键字的input和下边加的DIV层。

3、打开你自己的样式表css文件,在最后加入

<span class="sh_selector" style="color: rgb(34, 95, 45);">.suggestionsBox</span> <span class="sh_cbracket">{</span> <span class="sh_property" style="color: rgb(165, 42, 42);">position:</span><span class="sh_value" style="color: rgb(255, 0, 255);">relative</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">left:</span><span class="sh_value" style="color: rgb(255, 0, 255);">0px</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">width:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">250px</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">background:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">white</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">border:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">1px</span> <span class="sh_value" style="color: rgb(255, 0, 255);">solid</span> <span class="sh_string" style="color: rgb(255, 0, 255);">#dcdcdc</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">color:</span> <span class="sh_string" style="color: rgb(255, 0, 255);">#323232</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">z-index:</span><span class="sh_value" style="color: rgb(255, 0, 255);">999</span>; <span class="sh_cbracket">}</span> 
<span class="sh_selector" style="color: rgb(34, 95, 45);">.suggestionList</span> <span class="sh_cbracket">{</span> <span class="sh_property" style="color: rgb(165, 42, 42);">margin:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">0px</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">padding:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">0px</span>; <span class="sh_cbracket">}</span> 
<span class="sh_selector" style="color: rgb(34, 95, 45);">.suggestionList</span> li <span class="sh_cbracket">{</span> <span class="sh_property" style="color: rgb(165, 42, 42);">margin:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">0px</span> <span class="sh_value" style="color: rgb(255, 0, 255);">0px</span> <span class="sh_value" style="color: rgb(255, 0, 255);">3px</span> <span class="sh_value" style="color: rgb(255, 0, 255);">0px</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">position:</span><span class="sh_value" style="color: rgb(255, 0, 255);">relative</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">padding:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">3px</span>; <span class="sh_property" style="color: rgb(165, 42, 42);">cursor:</span> <span class="sh_value" style="color: rgb(255, 0, 255);">pointer</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">list-style:</span><span class="sh_value" style="color: rgb(255, 0, 255);">none</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">padding-left:</span><span class="sh_value" style="color: rgb(255, 0, 255);">5px</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">height:</span><span class="sh_value" style="color: rgb(255, 0, 255);">20px</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">overflow:</span><span class="sh_value" style="color: rgb(255, 0, 255);">hidden</span><span class="sh_cbracket">}</span> 
<span class="sh_selector" style="color: rgb(34, 95, 45);">.suggestionList</span> li<span class="sh_symbol">:</span>hover <span class="sh_cbracket">{</span> <span class="sh_property" style="color: rgb(165, 42, 42);">background-color:</span> <span class="sh_string" style="color: rgb(255, 0, 255);">#659CD8</span>; <span class="sh_cbracket">}</span> 
<span class="sh_selector" style="color: rgb(34, 95, 45);">.jr</span><span class="sh_cbracket">{</span><span class="sh_property" style="color: rgb(165, 42, 42);">position:</span><span class="sh_value" style="color: rgb(255, 0, 255);">absolute</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">top:</span><span class="sh_value" style="color: rgb(255, 0, 255);">9px</span>;<span class="sh_property" style="color: rgb(165, 42, 42);">right:</span><span class="sh_value" style="color: rgb(255, 0, 255);">-5px</span><span class="sh_cbracket">}</span> 

  此样式可以根据自己的网站定义噢。

  到这添加的代码就完了,然后下载search_list.rar,下载后解压得search_list.php到放入/plus目录下。在这个文件里有详细注解。可根据实际情况调整。所有结果都是由这个文件来返回的。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值