博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

百度跨域搜索demo

Posted on 2017-03-29 21:42  小易的博客  阅读(258)  评论(0编辑  收藏  举报
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
padding: 0;
margin: 0
}

.sousuo {
width: 300px;
height: 40px;
position: absolute;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
}

li {
list-style: none;
line-height: 30px;
padding: 0 10px;
}

li:hover {
background-color: #777;
color: #fff;
}

ul {
position: absolute;
display: none;
}
</style>
</head>
<body>
<input type="text" class="sousuo"/>
<ul class="ul"></ul>
<script src="../blog/javascripts/jquery-1.7.2.min.js"></script>
<script>

function position() {
var sousuo = $(".sousuo");
$(".ul").css({
top: sousuo.offset().top + sousuo.height() + 5,
left: sousuo.offset().left,
width: sousuo.css("width")
})
}
function demo(data) {
var ul = $('.ul');
var html = '';
if (data.s.length) {
ul.css("display",'block');
for (var i = 0; i<data.s.length; i++) {
html +='<li>'+data.s[i]+'</li>';
}
ul.html(html);
}
}
$(".ul").delegate("li",'click',function () {
location.href = 'https://www.baidu.com/s?wd='+$(this).text();
});
$(document).ready(function () {
position();
$('.sousuo').on('keyup', function () {
if (this.value) {
var script = document.createElement("script");
script.src = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+this.value+'&cb=demo';
document.body.appendChild(script);
}
});
});
window.onresize = function () {
position();
}
</script>
</body>
</html>