百度联想
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百度联想相关的知识,希望对你有一定的参考价值。
<!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>
以上是关于百度联想的主要内容,如果未能解决你的问题,请参考以下文章
Selenium2+python自动化34-获取百度输入联想词