利用跨域写搜索(智能提示)

本文介绍了一个使用HTML, CSS和JavaScript实现的淘宝搜索建议功能。通过监听输入框的输入事件,动态加载并显示与输入关键字相关的搜索建议。此功能增强了用户体验,使用户能够更快地找到他们想要的商品。

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

以淘宝为例

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			*{
				padding: 0;
				margin: 0;
			}
			a{
				text-decoration:none ;
				color: #000;
			}
			form{
				width: 400px;
				height: 32px;
				margin: 10px auto;
			}
			input{
				float: left;
				border: 0;
				outline: 0;
			}
			#txt{
				width: 346px;
				height: 30px;
				border: 1px solid #ccc;
				border-radius: 10px 0 0 10px;
			}
			#btn{
				background: orange;
				width: 50px;
				height: 32px;
				color: #fff;
				border-radius: 0 12px 12px 0;
			}
			#list{
				width: 400px;
				margin: 0px auto;
				list-style:none ;
				border: 1px solid #ccc;
				display: none;
			}
		</style>
	</head>
	<body>
		<form>
			<input type="text" id="txt" placeholder="请输入关键字"/>
			<input type="button" name="btn" id="btn" value="搜索" />
		</form>
		<ul id="list"></ul>
		
		<script type="text/javascript">
			var oTxt = document.getElementById("txt");
			var oList = document.getElementById("list");
			oTxt.oninput = function() {
				oList.style.display="block";
				var oScript = document.createElement("script");
				oScript.src = "https://suggest.taobao.com/sug?code=utf-8&q=" + oTxt.value + "&callback=data&area=c2c";
				document.body.appendChild(oScript);
			}
			
			function data(data){
				//console.log(data);
				var data1 = data.result;
				var str="";
				data1.forEach((item)=>{
					str += "<li><a href='https:/s.taobao.com/search?q="+item[0]+"'>"+item[0]+"</a></li>";
				});
				oList.innerHTML = str;
				
			}
		</script>
	</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值