forked from meethigher/music-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo05.html
79 lines (79 loc) · 1.85 KB
/
demo05.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
ul {
display:none;
}
ul li {
background-color: orange;
}
ul li.active {
background-color: pink;
}
</style>
</head>
<body>
<input type="search"><button>搜索</button>
<ul></ul>
<script src="https://cdn.jsdelivr.net/gh/meethigher/cdn@11/js/jquery.min.js"></script>
<script>
let arr=["说好不哭","从别后","勾指起誓"];
for(let i=0;i<arr.length;i++){
$("ul").append("<li>"+arr[i]+"</li>");
}
let content;
$("button").on("click",function (){
content=$("input").val();
console.log("搜索" + content);
});
let current=-1;
function keyDownEvent(e){
$("ul").show();
if(e.keyCode===40||e.which===40){
if(current>=$("ul li").length-1){
current=0;
}else{
current++;
}
$("ul li").each(function (index,ele){
if(index===current){
$(ele).addClass("active");
}else{
$(ele).removeClass("active");
}
});
$("input").val($(".active").text());
console.log(current);
}
if(e.keyCode===38||e.which===38){
if(current<=0){
current=$("ul li").length-1;
}else{
current--;
}
$("ul li").each(function (index,ele){
if(index===current){
$(ele).addClass("active");
}else{
$(ele).removeClass("active");
}
});
$("input").val($(".active").text());
console.log(current);
}
if(e.keyCode===13||e.which===13){
$("button").click();
$("ul").hide();
current=0;
}
}
$("input").on("keydown",function (e){
console.log("按下");
keyDownEvent(e);
});
</script>
</body>
</html>