自定义Autocomplete

本文介绍了一个使用JavaScript编写的自动完成功能的实现。通过创建AutoComplete类,实现了输入框与下拉提示框的交互,包括输入值的过滤、高亮显示、键盘事件处理等。此功能适用于前端开发中提高用户输入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

<!doctype html>
<html>
	<style>
		body {
			margin-left: 0px;
			margin-top: 0px;
			margin-right: 0px;
			margin-bottom: 0px;
		}

		.auto_hidden {
			width: 204px;
			border-top: 1px solid #333;
			border-bottom: 1px solid #333;
			border-left: 1px solid #333;
			border-right: 1px solid #333;
			position: absolute;
			display: none;
		}

		.auto_show {
			width: 204px;
			border-top: 1px solid #333;
			border-bottom: 1px solid #333;
			border-left: 1px solid #333;
			border-right: 1px solid #333;
			position: absolute;
			z-index: 9999;
			/* 设置对象的层叠顺序 */
			display: block;
		}

		.auto_onmouseover {
			color: #ffffff;
			background-color: highlight;
			width: 100%;
		}

		.auto_onmouseout {
			color: #000000;
			width: 100%;
			background-color: #ffffff;
		}
	</style>
	<script language="javascript">
		<!--
		var $ = function(id) {
			return "string" == typeof id ? document.getElementById(id) : id;
		}
		var Bind = function(object, fun) {
			return function() {
				return fun.apply(object, arguments);
			}
		}

		function AutoComplete(obj, autoObj, arr) {
			this.obj = $(obj); //输入框
			this.autoObj = $(autoObj); //DIV的根节点
			this.value_arr = arr; //不要包含重复值
			this.index = -1; //当前选中的DIV的索引
			this.search_value = ""; //保存当前搜索的字符
		}
		AutoComplete.prototype = {
			//初始化DIV的位置
			init: function() {
				this.autoObj.style.left = this.obj.offsetLeft + "px";
				this.autoObj.style.top = this.obj.offsetTop + this.obj.offsetHeight + "px";
				this.autoObj.style.width = this.obj.offsetWidth - 2 + "px"; //减去边框的长度2px  
			},
			//删除自动完成需要的所有DIV
			deleteDIV: function() {
				while (this.autoObj.hasChildNodes()) {
					this.autoObj.removeChild(this.autoObj.firstChild);
				}
				this.autoObj.className = "auto_hidden";
			},
			//设置值
			setValue: function(_this) {
				return function() {
					_this.obj.value = this.seq;
					_this.autoObj.className = "auto_hidden";
				}
			},
			//模拟鼠标移动至DIV时,DIV高亮
			autoOnmouseover: function(_this, _div_index) {
				return function() {
					_this.index = _div_index;
					var length = _this.autoObj.children.length;
					for (var j = 0; j < length; j++) {
						if (j != _this.index) {
							_this.autoObj.childNodes[j].className = 'auto_onmouseout';
						} else {
							_this.autoObj.childNodes[j].className = 'auto_onmouseover';
						}
					}
				}
			},
			//更改classname
			changeClassname: function(length) {
				for (var i = 0; i < length; i++) {
					if (i != this.index) {
						this.autoObj.childNodes[i].className = 'auto_onmouseout';
					} else {
						this.autoObj.childNodes[i].className = 'auto_onmouseover';
						this.obj.value = this.autoObj.childNodes[i].seq;
					}
				}
			},
			//响应键盘
			pressKey: function(event) {
				var length = this.autoObj.children.length;
				//光标键"↓"
				if (event.keyCode == 40) {
					++this.index;
					if (this.index > length) {
						this.index = 0;
					} else if (this.index == length) {
						this.obj.value = this.search_value;
					}
					this.changeClassname(length);
				}
				//光标键"↑"
				else if (event.keyCode == 38) {
					this.index--;
					if (this.index < -1) {
						this.index = length - 1;
					} else if (this.index == -1) {
						this.obj.value = this.search_value;
					}
					this.changeClassname(length);
				}
				//回车键
				else if (event.keyCode == 13) {
					this.autoObj.className = "auto_hidden";
					this.index = -1;
				} else {
					this.index = -1;
				}
			},
			//程序入口
			start: function(event) {
				if (event.keyCode != 13 && event.keyCode != 38 && event.keyCode != 40) {
					this.init();
					this.deleteDIV();
					this.search_value = this.obj.value;
					var valueArr = this.value_arr;
					valueArr.sort();
					if (this.obj.value.replace(/(^\s*)|(\s*$)/g, '') == "") {
						return;
					} //值为空,退出
					try {
						var reg = new RegExp("(" + this.obj.value + ")", "i");
					} catch (e) {
						return;
					}
					var div_index = 0; //记录创建的DIV的索引
					for (var i = 0; i < valueArr.length; i++) {
						if (reg.test(valueArr[i])) {
							var div = document.createElement("div");
							div.className = "auto_onmouseout";
							div.seq = valueArr[i];
							div.onclick = this.setValue(this);
							div.onmouseover = this.autoOnmouseover(this, div_index);
							div.innerHTML = valueArr[i].replace(reg, "<strong>$1</strong>"); //搜索到的字符粗体显示
							this.autoObj.appendChild(div);
							this.autoObj.className = "auto_show";
							div_index++;
						}
					}
				}
				this.pressKey(event);
				window.onresize = Bind(this, function() {
					this.init();
				});
			}
		}
		//
		-->
	</SCRIPT>
	<body>
		<h1 align="center">自动完成函数(Autocomplete Function)</h1>
		<div align="center"><input type="text" style="width:300px;height:20px;font-size:14pt;" id="o"
				onkeyup="autoComplete.start(event)"></div>
		<div class="auto_hidden" id="auto">
			<!--自动完成 DIV-->
		</div>
		<script>
			var autoComplete = new AutoComplete('o', 'auto', ['b0', 'b12', 'b22', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b2',
				'abd', 'ab', 'acd', 'accd', 'b1', 'cd', 'ccd', 'cbcv', 'cxf'
			]);
		</SCRIPT>
	</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值