仿百度的搜索下拉提示 http://www.cnblogs.com/forcertain/archive/2011/01/27/1946041.html ajax的應(yīng)用在當(dāng)今web項目上,到處都是最常見也用的最多的地方就應(yīng)該是登錄、表單和搜索提示了。 今天分享下自己用到的搜索下拉提示。 第一步,是前臺展示的時候: 2 | < input type = "text" id = "textword" onkeyup = "showtip(event,this);" onkeydown = "regword(this);" onclick = "showtip(event,this);event.cancelBubble = true;event.returnValue = false;" /> |
3 | < input type = "button" id = "btnsearch" /> |
5 | < ul id = "sosotip" onclick = "hiddtip()" ></ ul > |
1 | 第二步,是后臺發(fā)回的數(shù)據(jù)格式: |
1 | < li id = "litooltip1" onmouseover = "mousestyle(this,1)" onclick = "redirect(‘提示詞1’)" >< label >提示詞1</ label >< span >共被搜索10次</ span ></ li > |
2 | < li id = "litooltip2" onmouseover = "mousestyle(this,2)" onclick = "redirect(‘提示詞2’)" >< label >提示詞2</ label >< span >共被搜索6次</ span ></ li > |
3 | < li id = "litooltip3" onmouseover = "mousestyle(this,3)" onclick = "redirect(‘提示詞3’)" >< label >提示詞3</ label >< span >共被搜索2次</ span ></ li > |
服務(wù)器端直接傳回的是組織好的html代碼,這樣的話,會使傳輸時數(shù)據(jù)膨脹,但這樣的話,把比較的復(fù)雜的處理都放到的服務(wù)器一端,實現(xiàn)起來更方便和簡單。另外,至于樣式的定位和顯示,這里就不貼出來了,全憑自己興趣,想怎么顯示自己看著辦。 下面,就是重點,js代碼了: 002 | function hiddtip(){var tipul = document.getElementById("sosotip");tipul.style.display="none";} |
004 | function autotext(strtext){document.getElementById("textword").value=strtext;} |
006 | document.body.onclick=function(){hiddtip();}; |
009 | var preword=""; // 記錄鍵盤操作按下時的文本框值 |
010 | var current=0; // 現(xiàn)在選擇的提示列表的第幾項 |
011 | var staticword=""; // 記錄鍵盤操作按下時的文本框值,忽略上下鍵的操作 |
013 | // onkeydown觸發(fā)時,記錄此時記錄文本框的值(以便onkeyup時文本框的值比較決定是否請求服務(wù)器) |
014 | function regword(target) |
016 | var tempword = target.value.replace(//s/g,""); |
023 | // 顯示提示列表,文本框onkeyup和onclick時觸發(fā) |
024 | function showtip(oEvent,target) |
026 | var sword = target.value.replace(//s/g,""); // 此時文本框的值 |
027 | var ulcontainer = document.getElementById("sosotip"); //提示列表容器 |
030 | ulcontainer.style.display="none"; // 文本框值為空時,隱藏提示 |
032 | else if(sword.length <20) |
034 | if(sword != preword) // 操作后,文本框值改變 |
038 | if(oEvent.keyCode!="38" || oEvent.keyCode!="40") |
042 | ulcontainer.style.display="none"; |
043 | ulcontainer.innerHTML =""; |
046 | url:"Ashx/searchtip.ashx", |
048 | success:function(result) |
052 | ulcontainer.innerHTML = result; |
053 | ulcontainer.style.display="block"; |
058 | else if(ulcontainer.innerHTML != "")//操作后文本框詞未變 |
060 | ulcontainer.style.display="block"; |
061 | clearallstyle(); // 清除提示列表上的所有樣式 |
062 | if(oEvent.keyCode=="38") // 是鍵盤上的向上操作時 |
065 | if(current == -1) //達(dá)到列表最上方時選中最后一個 |
067 | var uls = document.getElementById("sosotip"); |
068 | var ochilds = uls.childNodes; |
069 | current = ochilds.length; |
070 | addlistyle(oEvent,ochilds[current-1]); //選中最后一個 |
074 | var fotar = document.getElementById("litooltip"+current); |
077 | addlistyle(oEvent,fotar); |
079 | else // 選中為第一個時再向上回到文本框 |
082 | autotext(staticword); |
086 | else if(oEvent.keyCode=="40") // 是鍵盤上的向下操作時 |
089 | var fotar = document.getElementById("litooltip"+current); |
092 | addlistyle(oEvent,fotar); |
096 | current=0;autotext(staticword); |
099 | else if(oEvent.keyCode=="13") // Enter鍵時,觸發(fā)按鈕 |
101 | document.getElementById("btnsearch ").click(); |
107 | function addlistyle(oEvent,target) |
109 | target.style.cssText="background-color:#36C;color:#fff;"; |
110 | autotext(target.childNodes[0].innerHTML); |
111 | oEvent.cancelBubble = true;oEvent.returnValue = false; |
114 | // 鼠標(biāo)與鍵盤的交互,鼠標(biāo)選中時按上下鍵可以按鼠標(biāo)選中的當(dāng)前上下 |
115 | function mousestyle(target,currflag) |
118 | target.style.cssText="background-color:#36C;cursor:pointer;color:#fff;"; |
122 | function clearallstyle() |
124 | var uls = document.getElementById("sosotip"); |
125 | var ochilds = uls.childNodes; |
126 | for(var i=0;i<ochilds.length;i++) |
128 | ochilds[i].style.cssText=""; |
131 | // 鼠標(biāo)點擊某一項時觸發(fā) |
132 | function redirect(word) |
134 | location.href="search.aspx?w="+encodeURI(word); |
其中ajax的請求用的是jquery。
|