码迷,mamicode.com
首页 > 其他好文 > 详细

百度联想

时间:2016-02-26 18:43:04      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>百度首页联想词</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<style>
* {
margin: 0;
padding: 0;
}

.search_box {
position: relative;
width: 310px;
margin: 30px auto;
font-size: 14px;
}

.search_box input {
border: 1px solid #ccc;
height: 18px;
line-height: 18px;
width: 298px;
padding: 5px;
box-shadow: inset 1px 1px 5px #e1e1e1;
outline: none;
}

.search_box ul {
position: absolute;
width: 308px;
left: 0;
top: 29px;
zoom: 1;
overflow: hidden;
border: 1px solid #CCC;
line-height: 1.8;
display: none;
background-color: white;
}

.search_box ul li {
text-indent: 5px;
float: left;
width: 100%;
cursor: default;
font-weight: bold;
}

.search_box ul .active {
background-color: #e1e1e1;
}
</style>
<script>
window.addEventListener(‘load‘, function() {
setTimeout(function() {
window.scrollTo(0, 1)
}, 0);
}, false)
</script>
</head>

<body>
<div class="search_box">
<input type="text" id="txt1" />
<ul id="datalist">
</ul>
</div>
<script>
window.onload = function() {
var oTxt = document.getElementById(‘txt1‘);
var oUl = document.getElementById(‘datalist‘);
var lastLen = 0;
var lastValue = ‘‘;
var oS = null;
var iNow = -1;

oTxt.onblur = function() {
oUl.style.display = ‘none‘;
}

oTxt.onkeydown = function() {
lastLen = this.value.length;
}

oTxt.onkeyup = function(evt) {
var evt = evt || event;

if (lastLen == this.value.length || evt.keyCode == 38 || evt.keyCode == 40) return;

if (oS) {
document.body.removeChild(oS)
}

lastValue = this.value;

iNow = -1;

var url = ‘http://suggestion.baidu.com/su?wd=‘ + lastValue + ‘&cb=succ‘;

oS = document.createElement(‘script‘);

oS.src = url;

document.body.appendChild(oS)
}

window.succ = function(json) {
if (json.s.length > 0) {
oUl.style.display = ‘block‘;
} else {
oUl.style.display = ‘none‘;
}

oUl.innerHTML = ‘‘;

for (var i = 0; i < json.s.length; i++) {
var oLi = document.createElement(‘li‘);

oLi.innerHTML = json.s[i];

oUl.appendChild(oLi);

oLi.onmouseover = function() {
this.className = ‘active‘;
}

oLi.onmouseout = function() {
this.className = ‘‘;
}

oLi.onmousedown = function() {
oTxt.value = this.innerHTML;
oUl.style.display = ‘none‘;
}
}

var aLi = oUl.getElementsByTagName(‘li‘);

function resetLi() {
for (var i = 0; i < aLi.length; i++) {
aLi[i].className = ‘‘;
}
}

function showTxt(n) {
aLi[n].className = ‘active‘;
oTxt.value = aLi[n].innerHTML;
}

document.onkeydown = function(evt) {
var evt = evt || event;

if (evt.keyCode == 38) {
iNow--
resetLi();
if (iNow == -2) {
showTxt(aLi.length - 1);
iNow = aLi.length - 1;
}
if (iNow == -1) {
oTxt.value = lastValue;
} else {
showTxt(iNow)
}
oUl.style.display = ‘block‘;
return false;
}

if (evt.keyCode == 40) {
iNow++
resetLi();
if (iNow == aLi.length) {
iNow = -1;
oTxt.value = lastValue;
} else {
showTxt(iNow)
}
oUl.style.display = ‘block‘;
}
}
}
}
</script>
</body>

</html>

百度联想

标签:

原文地址:http://www.cnblogs.com/mouse2417/p/5221441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!