<div class="codetitle"> <span><a style="CURSOR: pointer" data="7157" class="copybut" id="copybut7157" onclick="doCopy('code7157')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code7157"> <br><!DOCTYPE html> <br><html> <br><head> <br><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <br><title>关键词匹配搜索仿百度</title> <br><meta name="description" content=" 内容介绍不超过100个中文"> <br><meta name="keywords" content=" 内容相关关键词3-5个"> <br><style> <br>body, ul, li { margin: 0; padding: 0;} <br>body{ font-size:12px; font-family:sumsun,arial;background:#FFFFFF;} <br>.gover_search{ position:relative; z-index:99; height:63px; padding:15px 0 0 20px; border:1px solid #b8cfe6; border-bottom:0; background:url(../images/gover_search_bg.gif) repeat-x 0 0;} <br>.gover_search_form{height:36px;} <br>.gover_search .search_t{ float:left; width:112px; line-height:26px; color:#666;} <br>.gover_search .input_search_key{ float:left; width:462px; height:18px; padding:3px; margin-right:5px; border:1px solid #ccc; line-height:18px; background:#fff;} <br>.gover_search .search_btn{ float:left; width:68px; height:26px; overflow:hidden; border:1px solid #ccc; text-align:center; color:#ff3300; letter-spacing:5px; background:url(../images/gover_search_bg.gif) no-repeat 0 -79px; cursor:pointer; font-weight:bold;} <br>.gover_search .search_suggest{ position:absolute; z-index:999; left:132px; top:41px; width:468px; border:1px solid #ccc;border-top:none; display:none;color:#004080; } <br>.gover_search .search_suggest li{height:24px; overflow:hidden; padding-left:3px; line-height:24px; background:#fff; cursor:default;} <br>.gover_search .search_suggest li.hover{background:#ddd;} <br>.num_right{float:right;text-align:right;line-height:24px;padding-right:10px} <br></style> <br></head> <br><br><body> <br><br><div class="gover_search"> <br><div class="gover_search_form clearfix"> <br><span class="search_t">关键词匹配搜索</span> <br><input type="text" class="input_search_key" id="gover_search_key" placeholder="请输入关键词直接搜索" /> <br><button type="submit" class="search_btn">搜索</button> <br><div class="search_suggest" id="gov_search_suggest"> <br><ul> <br></ul> <br></div> <br></div> <br></div> <br><script type="text/javascript" src="http://jt.875.cn/js/jquery.js"></script> <br><br><script type="text/ javascript"> <br><br>//實作搜尋輸入框的輸入提示js類別<BR>function oSearchSuggest(searchFuc){ <BR>var input = $('#gover_search_key'); <BR>var suggestWrap = $('#gover_search_key'); <BR>var suggestWrap = $ ('#gov_search_suggest'); <BR>var key = ""; <BR>var init = function(){ <BR>input.bind('keyup',sendKeyWord); <BR>input.bind('blur' ,function(){setTimeout(hideSuggest,100);}) <BR>} <BR>var hideSuggest = function(){ <BR>suggestWrap.hide(); <br>} <br><BR><br>/送請求,根據關鍵字到後台查詢<br>var sendKeyWord = function(event){ <BR><BR>//鍵盤選擇下拉項目<BR>if(suggestWrap.css('display')=='block'&&event .keyCode == 38||event.keyCode == 40){ <BR>var current = suggestWrap.find('li.hover'); <BR>if(event.keyCode == 38){ <BR>if( current.length>0){ <BR>var prevLi = current.removeClass('hover').prev(); <BR>if(prevLi.length>0){ <BR>prevLi.addClass('hover'); <BR>input.val(prevLi.html()); <BR>} <BR>}else{ <BR>var last = suggestWrap.find('li:last'); <BR>last.addClass('hover '); <br>input.val(last.html()); <br>} <BR><BR>}else if(event.keyCode == 40){ <BR>if(current.length>0) { <BR>var nextLi = current.removeClass('hover').next(); <BR>if(nextLi.length>0){ <BR>nextLi.addClass('hover'); <BR>input.val (nextLi.html()); <BR>} <BR>}else{ <BR>var first = suggestWrap.find('li:first'); <BR>first.addClass('hover'); <BR> input.val(first.html()); <br>} <br>} <BR><BR>//輸入字元<BR>}else{ <BR>var valText = $.trim(input.val() ); <BR>if(valText ==''||valText==key){ <BR>return; <BR>} <BR>searchFuc(valText); <br>key = valText; <br>} <BR>} <BR>key = valText; <BR>} <BR><BR>} <BR>//請求回傳後,執行資料展示<br>this.dataDisplay = function(data){ <br>if(data.length<=0){ <BR>suggestWrap.hide() ; <BR>return; <BR>} <BR><BR>//往搜尋框下拉建議顯示欄中新增項目並顯示<BR>var li; <BR>var tmpFrag = document.createDocumentFragment(); <BR>var tmpFrag = document.createDocumentFragment(); <BR>suggestWrap.find('ul').html(''); <BR>for(var i=0; i<data.length; i ){ <br>li = document.createElement('LI'); <br>li.innerHTML = data[i]; <BR>tmpFrag.appendChild(li); <BR>} <BR>suggestWrap.find('ul').append(tmpFrag); <br>suggestWrap.show() ; <br><BR>//為下拉選項綁定滑鼠事件<BR>suggestWrap.find('li').hover(function(){ <BR>suggestWrap.find('li').removeClass('hover '); <BR>$(this).addClass('hover'); <BR><BR>},function(){ <BR>$(this).removeClass('hover'); <BR>}) .bind('click',function(){ <BR>$(this).find("span").remove(); <br>input.val(this.innerHTML); <br>suggestWrap.hide() ; <BR>}); <br>} <br>init(); <BR>}; <BR><BR>//實例化輸入提示的JS,參數為進行查詢操作時要呼叫的函數名稱<BR>var searchSuggest = new oSearchSuggest(sendKeyWordToBack); <BR><BR>//這是一個模似函數,實現向後台發送ajax查詢請求,並返回一個查詢結果數據,傳遞給前台的JS,再由前台JS來展示數據。本函數由程式設計師進行修改實作查詢的請求<BR>//參數為一個字串,是搜尋輸入框中目前的內容<BR>function sendKeyWordToBack(keyword){ <BR>/* var obj = { <BR>"keyword" : keyword <BR>}; <BR>$.ajax({ <BR>type: "POST", <BR>url: "${ctx}/front/suqiu2/search/prompt-keyword.action ", <BR>async:false, <BR>data: obj, <BR>dataType: "json", <BR>success: function(data){ <BR>//var json = eval("(" data " )"); <BR>var key=data.split(","); <BR>var aData = []; <BR>for(var i=0;i<key.length;i ){ <BR> //以下為根據輸入返回搜尋結果的模擬效果代碼,實際資料由後台傳回<BR>if(key[i]!=""){ <BR>aData.push(key[i]); <br> } <br>} <BR>//將傳回的資料傳遞給實作搜尋輸入框的輸入提示js類別<BR>searchSuggest.dataDisplay(aData); <BR>} <BR>}); */ <BR> <BR>//以下為根據輸入返回搜尋結果的模擬效果代碼,實際資料由後台傳回<BR>var aData = []; <BR>aData.push('<span class="num_right">約100個' keyword '回傳資料1'); <BR>aData.push('<span class="num_right">約200個' keyword '回傳資料2'); <BR>aData. push('<span class="num_right">約100個' keyword '回傳資料3'); <BR>aData.push('<span class="num_right">約50000個 ' keyword '回傳資料4'); <BR>aData.push('<span class="num_right">約1044個' keyword '2012是真的'); <BR>aData.push(' <span class="num_right">約100個' keyword '2012是假的'); <BR>aData.push('<span class="num_right">約100個' keyword '2012是真的'); <BR>aData.push('<span class="num_right">約100個' keyword '2012是假的'); <BR>//將傳回的數據傳遞給實作搜尋輸入框的輸入提示js類別</script> </div>searchSuggest.dataDisplay(aData); }