-
Notifications
You must be signed in to change notification settings - Fork 0
/
baiduSearch.html
104 lines (92 loc) · 2.5 KB
/
baiduSearch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!-- liyiqi 2017-08-18 -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>百度搜索框</title>
<style>
* {
margin: 0;
padding: 0;
text-decoration: none;
list-style: none;
}
#box {
width: 600px;
margin: 50px auto;
}
#text {
width: 400px;
height: 20px;
line-height: 20px;
}
#searchBtn {
width: 50px;
height: 24px;
}
ul {
position: relative;
display: none;
z-index: 10;
box-shadow: 0px 5px 10px #ccc;
}
a {
color: blue;
}
</style>
</head>
<body>
<div id="box">
<input type="text" id="text">
<input type="button" value="搜索" id="searchBtn">
<ul id="uu"></ul>
</div>
<script>
function xx(data) {
console.log(data); //看看传回来的数据形式
ul.innerHTML = '';
var arr = data.s; //发现s属性是传回来的数据
var df = document.createDocumentFragment(); //创建文档碎片
if (!data || !arr) {
ul.style.display = 'none';
} else {
ul.style.display = 'block';
arr.forEach(function (keyCode) { //遍历元素, 及插入节点
var oLi = document.createElement('li');
var oA = document.createElement('a');
oA.setAttribute('href', 'https://www.baidu.com/s?wd=' + keyCode);
oA.innerText = keyCode;
oLi.appendChild(oA);
df.appendChild(oLi);
});
ul.appendChild(df);
}
}
var ul = document.getElementById('uu');
var input = document.getElementById('text');
var searchBtn = document.getElementById('searchBtn');
input.onkeyup = function (event) {
var e = event || window.event;
console.log(e);
if (e.keyCode == 13 && input.value !== '') {
window.location = 'https://www.baidu.com/s?wd=' + encodeURI(input.value);
}
if (input.value == '') {
ul.style.display = 'none';
} else {
var oScript = document.createElement('script'); //通过script标签获取数据
oScript.src = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=' + input.value + '&cb=xx';
document.body.appendChild(oScript);
oScript.remove(); //获取完数据将标签删除
}
}
searchBtn.onclick = function () {
if (input.value == '') {
ul.style.display = 'none';
} else {
window.location = 'https://www.baidu.com/s?wd=' + encodeURI(input.value);
}
}
</script>
</body>
</html>