自己用PHP + js模仿的百度搜索时输入内容带提示的功能,运行的话需要PHP环境。也算是自己的一个练笔题吧,和大家分享一下

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
$aName = array("王成","王瑞","王祥","何武昌","何睿","何好","马兴","马成栋","张相","张祥");
$aId = array(88801,11144,2345,9023,12415,88021,11145,2344,11145,0376);
foreach($aName as $k =>$val)
{
$json\[$k\] = array(
'id' => $aId\[$k\],
'name' => $val
);
}

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*
\* 使用特定function对数组中所有元素做处理
\* @param string &$array 要处理的字符串
\* @param string $function 要执行的函数
\* @return boolean $apply\_to\_keys\_also 是否也应用到key上
\* @access public
\*
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
function arrayRecursive(&$array, $function, $apply\_to\_keys\_also = false)
{
static $recursive\_counter = 0;
if (++$recursive\_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is\_array($value)) {
arrayRecursive($array\[$key\], $function, $apply\_to\_keys\_also);
} else {
$array\[$key\] = $function($value);
}
if ($apply\_to\_keys\_also && is\_string($key)) {
$new\_key = $function($key);
if ($new\_key != $key) {
$array\[$new\_key\] = $array\[$key\];
unset($array\[$key\]);
}
}
}
$recursive\_counter--;
}
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*
\* 将数组转换为JSON字符串(兼容中文)
\* @param array $array 要转换的数组
\* @return string 转换得到的json字符串
\* @access public
\*
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json\_encode($array);
return urldecode($json);
}
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html;charset=gb2312" http-equiv="content-type" />
<style type="text/css">
.name {
cursor: pointer;
}

.selected {
background: #CCC;
}

.normal {
background: #FFF;
}
</style>
</head>
<table align="center">
<tr>
<td>输入测试:</td>
<td><input type="text" style="color:" id="neirong" onkeyup="tip(event,0)" /></td>
</tr>
</table>
<div id="showMessage" style="border:1px #666 solid;">
</div>
<script type="text/javascript">
window.onload = function () {
var tag = document.getElementById("showMessage");
tag.style.display = "none";
}
function tip(event) {
var aUserName = new Array();
var aTempName, aUserName, tag, sVal, sHtml, mesTag, selectedTag, didTag, widths;
var postion = new Array();
var sId = 0;//用于给每个名字加一个id的顺序
var whichType;//用户输入的类型,是数字呢还是中文。
aUserName = <? php echo JSON($json); ?>;
//aUserName = eval('(' + aTempName + ')');
tag = document.getElementById("neirong");
didTag = document.getElementById("did");
//输入框坐标获取
postion = getElementPos(tag);

//显示div坐标调整
mesTag = document.getElementById("showMessage");
mesTag.style.position = "absolute";
mesTag.style.left = postion\[0\]
mesTag.style.top = postion\[1\];
widths = tag.style.width + "px";
mesTag.width = widths;
var event = event window.event;
var sKeyCode = event.keyCode;
var aShangXia = new Array(37, 38, 39, 40);
var isDerection;
sVal = tag.value;
//以enter作为用户输入结束标志
if (13 != sKeyCode) {
for (var i in aShangXia) {
if (sKeyCode == aShangXia) {
isDerection = false;
break;
}
else {
isDerection = true;
}
}
if (isDerection) {
sHtml = '';
if ("" == sVal) {
mesTag.style.display = "none";
mesTag.innerHTML = sHtml;
return false;
}
else {
sHtml = '<table width="146px" border="0" id="showTable">';
if (checkVal(sVal)) {
for (var i in aUserName) {
if (0 <= aUserName\[i\].id.indexOf(sVal)) {
sHtml = sHtml + '<tr><td class="normal" onclick="clickToChoice(' + sId + ')" onmouseover="changeColor(true,' + sId + ')" onmouseout="changeColor(false,' + sId + ')" id="p\_' + sId + '" readonly="readonly">' + aUserName\[i\].id + '\_' + aUserName\[i\].name + '</td></tr>';
sId = sId + 1;
}
}
}
else {
for (var i in aUserName) {
if (0 <= aUserName\[i\].name.indexOf(sVal)) {
sHtml = sHtml + '<tr><td class="normal" onclick="clickToChoice(' + sId + ')" onmouseover="changeColor(true,' + sId + ')" onmouseout="changeColor(false,' + sId + ')" id="p\_' + sId + '" readonly="readonly">' + aUserName\[i\].id + '\_' + aUserName\[i\].name + '</td></tr>';
sId = sId + 1;
}
}
}
sHtml = sHtml + '</table>';
if (0 != sId) {
mesTag.style.display = "";
mesTag.innerHTML = sHtml;
sHtml = '';
}
else {
mesTag.innerHTML = '没有结果';
}
}
}
else {
if (38 == sKeyCode40 == sKeyCode) {
dance(sKeyCode, sVal, tag);
}
}
}
else {
if ('' != tag.value) {
mesTag.innerHTML = '';
showMessage.style.display = 'none';
}
else {
return false;
}
}
}

function checkVal(sVal) {
var patrn = /^\[0-9\]/;
if (patrn.exec(sVal)) {
return true;
}
else {
return false;
}
}
//获取用户通过点击的名字
function clickToChoice(sqnm) {
var choicedTag, mesTag, showTag;
choicedTag = document.getElementById("p\_" + sqnm);
mesTag = document.getElementById("neirong");
showTag = document.getElementById("showTable");
mesTag.value = choicedTag.innerHTML;
showMessage.style.display = 'none';
}

function dance(sKeyCode, sVal, tag) {
var danceTag = getElementsByClassName("normal");//这个是正常的行
var danceingTag = getElementsByClassName("selected");//这个是前一个被选中的行
var selectedTag, lastTag, showTag, selectedVal, currentNum, initial;
var rowNums, selectedNum = 0;
var danceLen = danceTag.length;
if (0 != danceingTag.length) {
showTag = document.getElementById("showTable");
rowNums = showTag.rows.length;
currentNum = danceingTag\[0\].id.split("\_") \[1\] \* 1;
switch (sKeyCode) {
case 40:
if (rowNums == (currentNum + 1)) {
selectedNum = 0;
}
else {
selectedNum = currentNum + 1;
}
break;
case 38:
if (0 == currentNum) {
selectedNum = rowNums - 1;
}
else {
selectedNum = currentNum - 1;
}
break;
}
}
else {
switch (sKeyCode) {
case 40:
currentNum = danceTag.length - 1;
selectedNum = 0;
break;
case 38:
currentNum = 0;
selectedNum = danceTag.length - 1;
break;
}
}

lastTag = document.getElementById("p\_" + currentNum);
lastTag.className = "normal";
selectedTag = document.getElementById("p\_" + selectedNum);
selectedVal = selectedTag.innerHTML;
selectedTag.className = "selected";
tag.value = selectedVal;

}
function changeColor(type, sqnm) {
var tag = document.getElementById("p\_" + sqnm);
if (type) {
tag.className = "selected";
}
else {
tag.className = "normal";
}
}

function getElementsByClassName(n) {
var classElements = \[\], allElements = document.getElementsByTagName('\*');
for (var i = 0; i < allElements.length; i++) {
if (allElements\[i\].className == n ) {
classElements\[classElements.length\] = allElements\[i\]; //某类集合
}
}
return classElements;
}



function getElementPos(tag) {
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') != -1);
var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
if (tag.parentNode === null tag.style.display == 'none') {
return false;
}
var parent = null;
var pos = \[\];
var box;
if (tag.getBoundingClientRect) {
box = tag.getBoundingClientRect();
var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
pos\[0\] = box.left + scrollLeft + "px";
pos\[1\] = box.bottom + scrollTop + "px";
return pos;
}
}
</script>
</body>

</html>

非原创