|
仿百度的搜索下拉提示 http://www.cnblogs.com/forcertain/archive/2011/01/27/1946041.html ajax的應(yīng)用在當(dāng)今web項(xiàng)目上,到處都是最常見(jiàn)也用的最多的地方就應(yīng)該是登錄、表單和搜索提示了。 今天分享下自己用到的搜索下拉提示。 第一步,是前臺(tái)展示的時(shí)候: 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 | 第二步,是后臺(tái)發(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代碼,這樣的話,會(huì)使傳輸時(shí)數(shù)據(jù)膨脹,但這樣的話,把比較的復(fù)雜的處理都放到的服務(wù)器一端,實(shí)現(xiàn)起來(lái)更方便和簡(jiǎn)單。另外,至于樣式的定位和顯示,這里就不貼出來(lái)了,全憑自己興趣,想怎么顯示自己看著辦。 下面,就是重點(diǎn),js代碼了: 002 | function hiddtip(){var tipul = document.getElementById("sosotip");tipul.style.display="none";} |
003 | // 鍵盤(pán)上下操作自動(dòng)改變輸入框的值 |
004 | function autotext(strtext){document.getElementById("textword").value=strtext;} |
005 | // 點(diǎn)擊頁(yè)面其它部分時(shí)隱藏提示框 |
006 | document.body.onclick=function(){hiddtip();}; |
009 | var preword=""; // 記錄鍵盤(pán)操作按下時(shí)的文本框值 |
010 | var current=0; // 現(xiàn)在選擇的提示列表的第幾項(xiàng) |
011 | var staticword=""; // 記錄鍵盤(pán)操作按下時(shí)的文本框值,忽略上下鍵的操作 |
013 | // onkeydown觸發(fā)時(shí),記錄此時(shí)記錄文本框的值(以便onkeyup時(shí)文本框的值比較決定是否請(qǐng)求服務(wù)器) |
014 | function regword(target) |
016 | var tempword = target.value.replace(//s/g,""); |
023 | // 顯示提示列表,文本框onkeyup和onclick時(shí)觸發(fā) |
024 | function showtip(oEvent,target) |
026 | var sword = target.value.replace(//s/g,""); // 此時(shí)文本框的值 |
027 | var ulcontainer = document.getElementById("sosotip"); //提示列表容器 |
030 | ulcontainer.style.display="none"; // 文本框值為空時(shí),隱藏提示 |
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") // 是鍵盤(pán)上的向上操作時(shí) |
065 | if(current == -1) //達(dá)到列表最上方時(shí)選中最后一個(gè) |
067 | var uls = document.getElementById("sosotip"); |
068 | var ochilds = uls.childNodes; |
069 | current = ochilds.length; |
070 | addlistyle(oEvent,ochilds[current-1]); //選中最后一個(gè) |
074 | var fotar = document.getElementById("litooltip"+current); |
077 | addlistyle(oEvent,fotar); |
079 | else // 選中為第一個(gè)時(shí)再向上回到文本框 |
082 | autotext(staticword); |
086 | else if(oEvent.keyCode=="40") // 是鍵盤(pán)上的向下操作時(shí) |
089 | var fotar = document.getElementById("litooltip"+current); |
092 | addlistyle(oEvent,fotar); |
094 | else //到第一個(gè)時(shí)回到文本框 |
096 | current=0;autotext(staticword); |
099 | else if(oEvent.keyCode=="13") // Enter鍵時(shí),觸發(fā)按鈕 |
101 | document.getElementById("btnsearch ").click(); |
106 | // 鍵盤(pán)上下時(shí)為選中的項(xiàng)加選中樣式 |
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)與鍵盤(pán)的交互,鼠標(biāo)選中時(shí)按上下鍵可以按鼠標(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)點(diǎn)擊某一項(xiàng)時(shí)觸發(fā) |
132 | function redirect(word) |
134 | location.href="search.aspx?w="+encodeURI(word); |
其中ajax的請(qǐng)求用的是jquery。
|