js实现搜索框智能提示上下移动效果
2011-06-08 23:09
936 查看
最近公司网站首页搜索框改进,需要在智能提示列表上加上支持键盘上下键移动的效果。
搞了一晚上,下面呈上纯javascript代码,没有用到jquery等其他类库。
以下仅供自己收藏,贴上来希望能起到抛砖引玉的作用。
搞了一晚上,下面呈上纯javascript代码,没有用到jquery等其他类库。
以下仅供自己收藏,贴上来希望能起到抛砖引玉的作用。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> ul,li{list-style-type:none;} </style> <script type="text/javascript" language="javascript"> var currentSelIndex = -1; var oldSelIndex = -1; function selectItem(keyword, event) { if (keyword == "") { document.getElementById("ulItems").style.display = "none"; return; } else { var liLength = document.getElementById("ulItems").getElementsByTagName("li").length; //获取列表数量 if ((event.keyCode == 38 || event.keyCode == 40) && document.getElementById("ulItems").style.display != "none") { if (liLength > 0) { oldSelIndex = currentSelIndex; //上移 if (event.keyCode == 38) { if (currentSelIndex == -1) { currentSelIndex = liLength - 1; } else { currentSelIndex = currentSelIndex - 1; if (currentSelIndex < 0) { currentSelIndex = liLength - 1; } } if (currentSelIndex != -1) { document.getElementById("li_" + currentSelIndex).style.backgroundColor = "#cbf3fd"; document.getElementById("txtKeyword").value = document.getElementById("li_" + currentSelIndex).innerText; } if (oldSelIndex != -1) { document.getElementById("li_" + oldSelIndex).style.backgroundColor = "#ffffff"; } } //下移 if (event.keyCode == 40) { if (currentSelIndex == liLength - 1) { currentSelIndex = 0; } else { currentSelIndex = currentSelIndex + 1; if (currentSelIndex > liLength - 1) { currentSelIndex = 0; } } if (currentSelIndex != -1) { document.getElementById("li_" + currentSelIndex).style.backgroundColor = "#cbf3fd"; document.getElementById("txtKeyword").value = document.getElementById("li_" + currentSelIndex).innerText; } if (oldSelIndex != -1) { document.getElementById("li_" + oldSelIndex).style.backgroundColor = "#ffffff"; } } } } else if (event.keyCode == 13) { if (document.getElementById("ulItems").style.display != "none" && liLength > 0 && currentSelIndex != -1) { document.getElementById("txtKeyword").value = document.getElementById("li_" + currentSelIndex).innerText; document.getElementById("ulItems").style.display = "none"; currentSelIndex = -1; oldSelIndex = -1; } } else { autoComplete(keyword); document.getElementById("ulItems").style.display = ""; currentSelIndex = -1; oldSelIndex = -1; } } } function autoComplete(keyword) { var liHtml0 = "<li id=\"li_0\">1</li><li id=\"li_1\">12</li><li id=\"li_2\">123</li><li id=\"li_3\">1234</li>"; var liHtml1 = "<li id=\"li_0\">12</li><li id=\"li_1\">123</li><li id=\"li_2\">1234</li>"; var liHtml2 = "<li id=\"li_0\">123</li><li id=\"li_1\">1234</li>"; var liHtml3 = "<li id=\"li_0\">1234</li>"; if (keyword == "1234") { document.getElementById("ulItems").innerHTML = liHtml3; } else if (keyword == "123") { document.getElementById("ulItems").innerHTML = liHtml2; } else if (keyword == "12") { document.getElementById("ulItems").innerHTML = liHtml1; } else if (keyword == "1") { document.getElementById("ulItems").innerHTML = liHtml0; } else { document.getElementById("ulItems").innerHTML = ""; } } </script> </head> <body> <input id="txtKeyword" type="text" onkeyup="selectItem(this.value, event)" style="width:200px;" /> <ul id="ulItems" style="display: none; border:1px solid #cecece; border-top:none; width:200px; padding:2px; margin:0px;"> </ul> </body> </html>
相关文章推荐
- js实现搜索框智能提示上下移动效果
- JS实现输入框类似百度搜索的智能提示效果
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- asp.net+js实现的ajax sugguest搜索提示效果
- JS实现搜索关键词的智能提示功能
- JS实现元素上下左右移动效果
- JS+CSS实现几个DIV层上下移动交换位置的效果
- asp.net+js实现的ajax sugguest搜索提示效果
- 利用jsonp跨域调用百度js实现搜索框智能提示
- 实现类似google搜索效果,文本框输入智能提示,没有用ajax控件和第三方控件,完全手写代码
- Js实现图片缩放上下左右移动效果
- 仿百度搜索智能提示(纯JS实现) (转)
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
- 仿百度搜索智能提示(纯JS实现)
- .NET中使用js实现百度搜索下拉提示效果[不是局部刷新,呜呜。。]
- js实现方块上下左右移动效果
- 实现类似google搜索效果,文本框输入智能提示,(经过改进 支持多个文本框的效果)
- .NET使用js制作百度搜索下拉提示效果(不是局部刷新)实现思路
- 使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码】
- 使用jsonp跨域调用百度js实现搜索框智能提示(转)