今天给大家带来的事一个简单的百度的历史搜索框,大家在搜索东西的时候,百度会自动给你显示你最近搜索过的一些东西,那这个拿 js 怎么做呢?
我们一起来学习吧
这是一个 html 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" type="images/x-icon" href="favicon.ico">
<title>百度一下,你就知道</title>
<link rel="stylesheet" type="text/CSS" href="css/index.css"/>
</head>
<body>
<header>
<div class="hea_left">
<a href="#">太原:</a>
<a href="#"><img src="img/a1.png"/ class="weather"> 7℃</a>
<a href="#"><span class="gr">轻度</span></a>
<a href="#">105 |</a>
<a href="#" class="a1">换肤</a>
<a href="#" class="a1">消息提示</a>
<a href="#" class="a1">显示频道</a>
</div>
<div class="hea_rig">
<a href="#" class="a2">新闻</a>
<a href="#" class="a2">hao 123</a>
<a href="#" class="a2">地图</a>
<a href="#" class="a2">视频</a>
<a href="#" class="a2">贴吧</a>
<a href="#" class="a2">学术</a>
<a href="#" class="a2">登陆</a>
<a href="#" class="a2">设置</a>
<a href="#" class="a3">更多产品</a>
</div>
</header>
<div id="logo">
<img src="img/superlogo.png"/>
</div>
<div class="box">
<input type="text" name="" id="inp" value=""/>
<div class="xj"></div>
<button id="but" onclick="onclickBut()">百度一下</button>
<ul>
<li>中共十九届二中全会公报公布</li>
<li>特朗普就任一周年 美国联邦政府正式"关门"</li>
<li>医生拄着拐杖做手术:病人多医生少,能做就做</li>
</ul>
</div>
<div class="footer">
<a href="" class="foot_a">设为首页</a>
<span class="foot_text">@2018 Baidu</span>
<a href="" class="foot_a">使用百度前必读</a>
<a href="" class="foot_a">意见反馈</a>
<span class="foot_text">京ICP证030173号</span>
<img src="img/copy_rignt_24.png"/>
<br />
<img src="img/icon-police.png" class="beian"/><span class="foot_text">京公网安备11000002000001号</span>
</div>
<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
这是一个 Css 页面:
*{padding: 0;margin: 0;}
body,html{width: 100%;height: 100%;background: url(../img/12.jpg) no-repeat;background-size:100% 100%; overflow: hidden;}
a{text-decoration: none;color: #fff;}
header{width: 100%;height: 32px;background-color: rgba(15,25,50,.3);line-height: 32px;padding-left:10px;font-size: 14px;}
.hea_left{float: left;}
.hea_rig{float: right;}
.gr{color: #badc00;font-weight: bold;}
.a1{margin-left: 10px;text-decoration: underline;font-size: 12px;}
.a2{margin-right: 20px;text-decoration: underline;font-size: 14px;font-weight: bold;}
.a3{display: block;float: right;width: 100px;height: 32px;background: #398bfb;text-align: center;line-height: 32px;}
#logo{width: 350px;height: 148px;margin: 6% auto;margin-bottom:2%;}
#logo img{cursor: pointer;width: 100%;height: 100%;}
.box{width: 660px;height: 300px;margin: 0 auto;position: relative; }
.xj{cursor: pointer;width: 24px;height: 22px;position: absolute;right:140px;top: 10px;background: url(../img/pir.png);background-size: 100% 100%;}
.xj:hover{background: url(../img/xj.png);background-size: 100% 100%;}
#inp{box-sizing: border-box;width: 536px;height: 45px;border: none;text-indent: 1em;}
#but{width: 120px;height: 45px;background: url(../img/an_bg.jpg) no-repeat;background-size: 100% 100%;border: none;margin-left:-4px;margin-top:-4px ;cursor:pointer;}
#but:focus{background: url(../img/an1_bg.jpg) no-repeat;background-size: 100% 100%;}
ul{width: 536px;background: #fff;display: none;}
li{list-style: none;line-height: 40px;text-indent: 1em;}
li:hover{background: #eee;cursor: pointer;}
.footer{width:100%;text-align: center;}
.foot_a{margin-right: 4px;text-decoration: underline;font-size: 12px;}
.foot_text{color: #fff;font-size: 12px;}
.beian{display: inline-block;margin-right: 6px;vertical-align: middle;}
最后就是 js 的代码了 ,方法有很多哦
//方法1
var but = document.getElementById("but"); //获取button百度按钮框
var arr = document.getElementsByTagName('li'); //获取要显示的li
var inp = document.getElementById("inp"); //获要输入的内容的input
var ul = document.getElementsByTagName('ul')[0]; //获取ul
function onclickBut(){ //
for (var i=2;i>=0;i--){
if(i==0){
arr[i].innerHTML=inp.value;
}else{
arr[i].innerHTML=arr[i-1].innerHTML;
}
}
//将百度作为连接,传入value的内容,并跳入新的页面
window.location.href ="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd="+inp.value;
}
//input和button聚焦和失焦的状态
inp.onfocus = but.onfocus = function(){
ul.style.display = 'block';
}
inp.onblur = but.onblur = function(){
ul.style.display = 'none';
}
//在input框中回车时
inp.onkeydown = function(Ent){
if(Ent.keyCode == 13){
onclickBut();
}
}
//点击输入过的value内容直接在input框显示
for(var x of arr){
x.onmousedown = look ;
}
function look(){
inp.value = this.innerHTML;
}
//数组
// var a = new Array();
// var but = document.getElementById("but");
// var inp = document.getElementById("inp");
// var lis = document.getElementsByTagName('li');
//
// but.onfocus = function(){
// var inps = inp.value;
// a.unshift(inps);
// a.length = 3;
// console.log(a);
// for(i=0;i<a.length;i++){
// if(a[i]!=undefined)
// lis[i].innerHTML = a[i];
// }
// }
我只是简略的写了一些,如果你有更好的方法记得我们一起探讨哦~~~
另外,以上代码如果哪里我 bug 随时欢迎帮我指正,谢谢
来源: https://www.cnblogs.com/zxnn/p/8324855.html