<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> <br> <br> <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> <br><br> <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 = $('#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>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>} <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>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类 <BR>searchSuggest.dataDisplay(aData); <BR>} <BR></script> <br> <br> <br> </div>