Repository navigation
Expand file tree
/
Copy pathlist_by_jquery.html
More file actions
114 lines (112 loc) · 2.82 KB
/
Copy pathlist_by_jquery.html
File metadata and controls
114 lines (112 loc) · 2.82 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态列表</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #000;
color: #fff;
}
#app {
width: 40%;
margin: 20px auto;
}
#fruits>li {
width: 90%;
height: 50px;
background-color: #6ca;
margin: 4px 0;
text-align: center;
font-size: 20px;
list-style-type: none;
line-height: 50px;
}
#fruits>li>a {
float: right;
color: #fff;
text-decoration: none;
margin-right: 10px;
}
#fruits+div {
margin-top: 20px;
}
#fname {
width: 70%;
height: 40px;
color: #fff;
border-radius: 8px;
border: none;
outline: none;
font-size: 20px;
text-align: center;
vertical-align: middle;
background-color: #999;
}
#ok {
width: 19%;
height: 40px;
color: #fff;
background-color: #a45;
border: none;
outline: none;
font-size: 16px;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="app">
<ul id="fruits">
<li>苹果<a href="">×</a></li>
<li>香蕉<a href="">×</a></li>
<li>榴莲<a href="">×</a></li>
<li>火龙果<a href="">×</a></li>
</ul>
<div>
<input type="text" id="fname">
<button id="ok">确定</button>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
// 1. $函数的参数是一个函数,该函数是页面加载完成后执行的回调函数
$(() => {
function removeItem(evt) {
evt.preventDefault()
// 4. $函数的参数是原生JavaScript对象,返回该原生JavaScript对象对应的jQuery对象
$(evt.target).parent().remove()
}
function addItem(evt) {
let fname = $('#fname').val().trim()
if (fname.length > 0) {
$('#fruits').append(
// 3. $函数的参数是标签字符串,创建对应的标签元素并返回jQuery对象
$('<li>').text(fname).append(
$('<a>').attr('href', '').text('×')
.on('click', removeItem)
)
)
}
$('#fname').val('')
// jQuery对象通过下标运算或get方法可以获得与之对应的原生JavaScript对象
// input.get(0).focus()
$('#fname')[0].focus()
}
// 2. $函数的参数是选择器字符串,返回对应元素的jQuery对象
$('#fruits a').on('click', removeItem)
$('#ok').on('click', addItem)
$('#fname').on('keydown', (evt) => {
let code = evt.keyCode || evt.which
if (code == 13) {
addItem(evt)
}
})
})
</script>
</body>
</html>