代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Js
】
文本框提示和自动完成
作者:
未松
/ 发布于
2016/4/25
/
971
1.模仿大型网站自动提示,就是输入“苹果”,在水果类中搜索,html代码如下: <div id="searchTips" style="display:none;width:291px;height:auto;position:absolute;z-index:9001;"> <ul></ul> <input type="hidden" id="hidSearchOption" /> </div> js代码如下: //显示搜索下拉选择项 function showTips(searchInput) { var top = searchInput.offsetTop+32; var left = searchInput.offsetLeft; var width = searchInput.offsetWidth; var ulStringFormat = "<li onclick='selectOption(this)' onmouseover='javascript:this.style.backgroundColor=\"#A9A9A9\"' onmouseout='javascript:this.style.backgroundColor=\"#666666\"' style='width:220px;height:25px;background-color: #666666;display:block;'><span style='float:left;text-indent:6px'>" + $("#searchvalue").val() + "</span><span id='{0}' style='float:right;padding-right:1px;fond-size:10px;'>{1}</span></li>"; while (searchInput = searchInput.offsetParent) { top += searchInput.offsetTop; left += searchInput.offsetLeft; } if ($("#searchvalue").val().length > 0) { $("#searchTips").css({ "left": left + "px", "top": top + "px" }).show(); $("#searchTips>ul").empty().append(kendo.format(ulStringFormat, "sFlightNo","航班号")).append(kendo.format(ulStringFormat, "sTailNo", "机号")); } else { searchInputBlur(); } } function fillvalue(value) { alert($(value).find("span:first").html()); searchInputBlur(); } function searchInputBlur() { $("#searchTips").hide(); } function selectOption(li) { $("#hidSearchOption").val(($(li).find("span:last").attr("id"))); searchInputBlur(); } //搜索 function searchContent() { var url = "/Manager/DutyManager?BeginTime=" + getTodayBeginUTCTime() + "&EndTime=" + getTodayEndUTCTime() var option = $("#hidSearchOption").val(); var value = $("#searchvalue").val().trim(); switch (option) { case "sFlightNo": url += "&FlightNo=" + value; break; case "sTailNo": url += "&TailNo=" + value; default: } openWindow(url); } 2.带上下左右键的 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SimulateUpDownKeySelect.html</title> <style type="text/css"> #divSelect {border:1px solid red; width:208px !important;width:210px;} #divSelect ul {width:200px;margin:3px; margin-left:-35px;*margin-left:3px;overflow:hidden} #divSelect ul li {float:left; list-style-type:none;width:45px;height:14px;line-height:20px;font:14px arial;text-align:center;padding:2px} #divSelect li:hover {background :green;cursor:pointer} #txtInput {width:205px;} </style> </head> <body> <form method="post" action="##"> <input type="text" id="txtInput" value="" autocomplete="off" onkeydown="if(event.keyCode==13)return false;" /> <!-- 防止回车键触发表单提交 onKeyPress --> <div id="divSelect"> </div> <script type="text/javascript"> var list = "<ul>" list += "<li>123a</li><li>战争片</li><li>动作片</li><li>爱情片</li><li>剧情片</li><li>记录片</li><li>fbac</li><li>bbca</li><li>defe</li><li>gzza</li><li>恐怖片</li><li>古装片</li><li>电视剧</li><li>读书</li><li >小说</li><li>作品集</li><li>历史</li><li>诗歌</li><li >散文</li><li>军事</li>"; list += "</ul>" document.getElementById('divSelect').innerHTML = list; </script> </form> <script type="text/javascript"> function $(sId) { return document.getElementById(sId); } function clearSelectedOptBgColor(target) { if (target.seletedIndex >= 0) target.options[target.seletedIndex].style.background = ""; } function setSelectedOptBgColor(target) { target.options[target.seletedIndex].style.background = "green"; } var upKeyCode = 38; var downKeyCode = 40; var enterKeyCode = 13; var oInput = $("txtInput"); oInput.options = $("divSelect").getElementsByTagName("li"); oInput.seletedIndex = -1; oInput.focus(); //oInput.onKeyPress{} oInput.onkeyup = function (event) { if (event == undefined) event = window.event; switch (event.keyCode) { case 37: clearSelectedOptBgColor(this); this.seletedIndex--; if (this.seletedIndex < 0) this.seletedIndex = this.options.length - 1; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 38: clearSelectedOptBgColor(this); this.seletedIndex = this.seletedIndex - 4; if (this.seletedIndex < 0) this.seletedIndex = this.options.length - 1; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 39: clearSelectedOptBgColor(this); this.seletedIndex++; if (this.seletedIndex >= this.options.length) this.seletedIndex = 0; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case 40: clearSelectedOptBgColor(this); this.seletedIndex = this.seletedIndex + 4; if (this.seletedIndex >= this.options.length) this.seletedIndex = 0; this.value = this.options[this.seletedIndex].innerHTML; setSelectedOptBgColor(this); break; case enterKeyCode: this.value = this.options[this.seletedIndex].innerHTML; //alert('aa') break; } }; oInput.onblur = function () { clearSelectedOptBgColor(this); this.seletedIndex = 1; }; </script> </body> </html> 3.下面这个只带上下键提示,写的比较规范 <head> <title></title> <style type="text/css"> #autoCompleteWp { border: 1px solid #ddd; padding: 20px; margin: 100px auto; width: 300px; position: relative; } #datalist { position: absolute; left: 0; top: 56px; background: #f8f8f8; z-index: 2; border: 1px solid #FCC; padding: 15px; display: none; } #datalist li { line-height: 1.6; list-style: none; cursor: pointer; } .on { background: #06C; color: #fff; } </style> </head> <body> <div id="autoCompleteWp"> 请输入邮箱: <input id="autoComplete" type="text" /> <ul id="datalist"> </ul> </div> <script type="text/javascript"> var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; var Class = { create: function () { return function () { this.initialize.apply(this, arguments); } } } function addEventHandler(oTarget, sEventType, fnHandler) { if (oTarget.addEventListener) { oTarget.addEventListener(sEventType, fnHandler, false); } else if (oTarget.attachEvent) { oTarget.attachEvent("on" + sEventType, fnHandler); } else { oTarget["on" + sEventType] = fnHandler; } }; function removeEventHandler(oTarget, sEventType, fnHandler) { if (oTarget.removeEventListener) { oTarget.removeEventListener(sEventType, fnHandler, false); } else if (oTarget.detachEvent) { oTarget.detachEvent("on" + sEventType, fnHandler); } else { oTarget["on" + sEventType] = null; } } String.prototype.trim = function () { return this.replace(/^\s+|\s+$/g, ''); } //自动完成类 var autoComplete = Class.create(); autoComplete.prototype = { initialize: function (obj, databox, data) { _this = this; this.input = $(obj); this.data = data; this.index = 0; this.datalist = $(databox).getElementsByTagName('li'); addEventHandler(this.input, 'keyup', this.writting); }, writting: function (e) { var e = e || window.event; var keyCode = e.keyCode || e.charCode; //兼容ie firefox等事件 switch (keyCode) { case 38: //上 _this.index--; if (_this.index < 0) { _this.index = $('datalist').getElementsByTagName('li').length - 1 } if ($('datalist').getElementsByTagName('li').length > 0) { for (var k = 0; k < $('datalist').getElementsByTagName('li').length; k++) { $('datalist').getElementsByTagName('li')[k].className = '' } } $('datalist').getElementsByTagName('li')[_this.index].className = "on"; break; case 40: _this.index++; if (_this.index > $('datalist').getElementsByTagName('li').length - 1) { _this.index = 0; } if ($('datalist').getElementsByTagName('li').length > 0) { for (var k = 0; k < $('datalist').getElementsByTagName('li').length; k++) { $('datalist').getElementsByTagName('li')[k].className = '' } } $('datalist').getElementsByTagName('li')[_this.index].className = "on" break; //回车 case 13: _this.input.value = $('datalist').getElementsByTagName('li')[_this.index].innerHTML.replace(/<\/?strong>/gi, '') //获取html,正则过滤标签,只取其值 $('datalist').style.display = 'none' break; default: var temp = []; var val = _this.input.value.trim(); if (val == '') { $('datalist').style.display = 'none' return; } //如果输入字符包含\会导致正则表达式出错,转换成\\即可 [ ] 等也需要转义 if (val.indexOf('\\') != -1) { val = val.replace(/\\/g, '\\\\') } var reg = new RegExp('^(' + val + ')'); //利用正则把匹配到的数据 加入数组 for (var i = 0; i < _this.data.length; i++) { //如果匹配到 if (reg.test(_this.data[i])) { thedata = _this.data[i].replace(reg, '<strong>$1</strong>'); //利用js正则式中replace中的占位符特性 temp.push('<li title="' + i + '">' + thedata + '</li>') } else { $('datalist').style.display = 'none' } } //如果匹配到数据 if (temp.length > 0) { $('datalist').innerHTML = temp.join(''); $('datalist').style.display = 'block'; var li = $('datalist').getElementsByTagName('li'); li[0].className = "on"; _this.index = 0; for (var i = 0; i < li.length; i++) { li[i].onmouseover = function () { for (var i = 0; i < li.length; i++) { li[i].className = ''; } this.className = 'on'; _this.index = this.getAttribute('title'); } //鼠标点击 li[i].onclick = function () { _this.input.value = this.innerHTML.replace(/<\/?strong>/gi, '') //获取html,正则过滤标签,只取其值 $('datalist').style.display = 'none' } } } else { $('datalist').innerHTML = ''; } break; } } } var data = ['yaofjaa@vip.qq.com', 'yaojaa008@163.com', 'yaojaa@yahoo.com', 'yaoj4aa@sohu.com.com', 'yaojaa@vipf.qq.com', 'yfaojaa@163.com', 'yaojfaa@sina.com', 'yaojaa@yahoo.com', 'yaojaa@sohu.com'] var auto = new autoComplete('autoComplete', 'datalist', data) </script> </body> </html>
试试其它关键字
同语言下
.
Jquery搜索框获取回车事件
.
H5页面添加倒计时,然后自动跳转
.
通过user-agent判断h5页面是在哪个手机App(QQ、微信
.
nginx 禁止未绑定的域名访问
.
JavaScript 获取按键,并屏蔽系统 Window 事件
.
H5之只允许微信浏览器打开,禁止从外部浏览器访问
.
微信打开网址添加在浏览器中打开提示的办法
.
实现JS复制、粘贴,Chrome/Firefox下可用
.
video视频播放,play()、pause()、duration时长、onen
.
HTML5实现MP3上传前的预览和播放时长的获取
可能有用的
.
Jquery搜索框获取回车事件
.
H5页面添加倒计时,然后自动跳转
.
通过user-agent判断h5页面是在哪个手机App(QQ、微信
.
nginx 禁止未绑定的域名访问
.
JavaScript 获取按键,并屏蔽系统 Window 事件
.
H5之只允许微信浏览器打开,禁止从外部浏览器访问
.
微信打开网址添加在浏览器中打开提示的办法
.
实现JS复制、粘贴,Chrome/Firefox下可用
.
video视频播放,play()、pause()、duration时长、onen
.
HTML5实现MP3上传前的预览和播放时长的获取
未松
贡献的其它代码
(
10
)
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
保存两位小数点
.
Handler相关
.
实现点击复制按钮或链接自动复制网址内容
.
实现指定时间段的工作天数
.
文本框提示和自动完成
.
提取网页json页面(不包括解析)
.
最短路径(单源dijkstra_bfs邻接表形式)
.
添加打印
.
SQL日期时间转为字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3