js百度搜索框功能模拟

这里我们就废话不多说了,直接上代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}
			
			#box {
				margin: 200px;
			}
			
			#txt {
				height: 30px;
				width: 320px;
			}
			
			#btn {
				width: 80px;
				height: 30px;
				border: none;
			}
		</style>
	</head>

	<body>
		<div id="box">
			<input type="text" id="txt" value="" />
			<input type="button" id="btn" value="搜索" />
		</div>
	</body>
	<script type="text/javascript">
		//  调用my$()方法,返回该id名的元素
		function my$(id) {
			return document.getElementById(id);
		}
		//	给元素添加内容    ---兼容处理版本
		function setInnerText(element, text) {
			if(typeof element.textContent == 'undefined') {
				element.innerText = text;
			} else {
				element.textContent = text;
			}
		}
	</script>
	<script type="text/javascript">
		//	定义关键字
		var keyword = ['一只小猫', '两只小猫', '一只小狗', '三只小狗', '一只羊', '两只羊', '三只羊', '百度一下', '百度两下'];
		my$('txt').onkeyup = function() {
			if(my$('dv')) { //  每当键盘抬起时,判断该元素是否存在,如果存在则删除这个元素
				my$("box").removeChild(my$('dv'));
			}
			var arrs = []; //定义空数组,存放满足条件的数组元素
			for(var i = 0; i < keyword.length; i++) {
				if(keyword[i].indexOf(this.value) == 0) {
					arrs.push(keyword[i]);
				}
			}
			// 下面这个this指向的是my$('txt')
			if(this.value == '' || arrs.length == 0) {
				if(my$('dv')) {
					my$("box").removeChild(my$('dv'));
				}
				return;
			}
			//创建存放结果的div,并设置样式,最后加入我们内容的下面
			var cdv = document.createElement('div');
			cdv.id = 'dv';
			cdv.style.width = '320px';
			cdv.style.border = '1px solid green';
			my$("box").appendChild(cdv);
			//	遍历结果
			for(var i = 0; i < arrs.length; i++) {
				var pds = document.createElement('p');
				cdv.appendChild(pds);
				setInnerText(pds, arrs[i]);
				//	鼠标悬停换颜色
				pds.onmouseover = function() {
					this.style.background = 'yellow';
				}
				pds.onmouseout = function() {
					this.style.background = '';
				}
			}
		}
	</script>
</html>

好啦,搜索框简单的功能就做完啦,本篇文章到这就结束咯,拜拜啦~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于Vue3实现类似于百度搜索框的示例代码: HTML模板: ``` <template> <div class="search-box"> <div class="search-input"> <input type="text" v-model="keyword" @input="search" /> <button @click="submit">搜索</button> </div> <ul class="search-list" v-show="showList"> <li v-for="(item, index) in searchList" :key="index" @click="selectItem(item)">{{ item }}</li> </ul> </div> </template> ``` CSS样式: ``` .search-box { position: relative; width: 400px; height: 40px; margin: 0 auto; } .search-input { position: relative; width: 100%; height: 100%; } .search-input input { width: 100%; height: 100%; border: none; outline: none; box-shadow: 0 0 3px rgba(0, 0, 0, 0.2); border-radius: 20px; padding: 0 20px; font-size: 16px; } .search-input button { position: absolute; top: 0; right: 0; width: 80px; height: 100%; background-color: #3385ff; border: none; outline: none; color: #fff; font-size: 16px; border-top-right-radius: 20px; border-bottom-right-radius: 20px; cursor: pointer; } .search-list { position: absolute; top: 40px; left: 0; width: 100%; max-height: 200px; overflow-y: auto; background-color: #fff; border: 1px solid #ccc; border-radius: 3px; box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1); z-index: 99; } .search-list li { padding: 10px; cursor: pointer; } .search-list li:hover { background-color: #f2f2f2; } ``` JavaScript代码: ``` <script> import { reactive } from 'vue'; export default { setup() { const state = reactive({ keyword: '', searchList: [], showList: false, }); // 模拟搜索函数 const searchFn = () => { const list = []; for (let i = 0; i < 10; i++) { list.push(state.keyword + i); } state.searchList = list; state.showList = true; }; // 监听输入框的变化,并触发搜索函数 const search = () => { if (state.keyword.trim() === '') { state.showList = false; state.searchList = []; return; } searchFn(); }; // 提交搜索 const submit = () => { if (state.keyword.trim() === '') { return; } // TODO: 处理搜索结果 }; // 点击搜索列表项 const selectItem = (item) => { state.keyword = item; state.showList = false; }; return { keyword: state.keyword, searchList: state.searchList, showList: state.showList, search, submit, selectItem, }; }, }; </script> ``` 以上代码实现了一个简单的搜索框,包括输入框、搜索按钮和搜索结果列表。当输入框内容发生变化时,会触发搜索函数,搜索结果会显示在下拉列表中。用户可以通过点击下拉列表中的项来选择搜索关键字,也可以直接点击搜索按钮提交搜索请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值