-
Notifications
You must be signed in to change notification settings - Fork 1
/
jq__traversing2.html
214 lines (185 loc) · 5.83 KB
/
jq__traversing2.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
h3 {
text-align: center;
}
table {
width: 80%;
border-collapse: collapse;
margin: 0 auto;
}
th,
td {
border: 1px solid black;
text-align: center;
padding: 10px;
}
div {
margin: 0 auto;
text-align: center;
}
button {
width: 100px;
margin-right: 50px;
border-radius: 10px;
}
.green {
background-color: lightgreen;
}
.reset {
background-color: skyblue;
}
.red {
background-color: pink;
}
</style>
</head>
<body>
<!-- jQuery Traversing -->
<!--往回找-->
<!--next_prev_first_index_eq()-->
<!--find只會找到第一格-->
<h3>Traversing 紅綠燈</h3>
<hr>
<div>
<button class="firstBtn" type="button">第一組</button>
<button class="green" type="button">綠燈</button>
<button class="reset" type="button">重置</button>
<button class="red" type="button">紅燈</button>
<button class="lastBtn" type="button">最後組</button>
</div>
<hr>
<table id="myTable">
<tr>
<th>id</th>
<th>姓名</th>
<th>電話</th>
</tr>
<tr>
<td>1</td>
<td> <span class="myName">amy</span></td>
<td>0911-111-111</td>
</tr>
<tr>
<td>2</td>
<td><span class="myName">bob</span></td>
<td>0922-222-222</td>
</tr>
<tr>
<td>3</td>
<td><span class="myName">cat</span></td>
<td>0944-444-444</td>
</tr>
<tr>
<td>4</td>
<td><span class="myName">dog</span></td>
<td>0955-555-555</td>
</tr>
<tr>
<td>5</td>
<td><span class="myName">egg</span></td>
<td>0966-666-666</td>
</tr>
<tr>
<td>6</td>
<td><span class="myName">fox</span></td>
<td>0977-777-777</td>
</tr>
<tr>
<td>7</td>
<td><span class="myName">girl</span></td>
<td>0988-888-888</td>
</tr>
<tr>
<td>8</td>
<td><span class="myName">hat</span></td>
<td>0999-999-999</td>
</tr>
<tr>
<td>9</td>
<td><span class="myName">ice</span></td>
<td>0911-111-222-333</td>
</tr>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script>
$(document).ready(function () {
// 練習一 紅綠燈
// green id 1 3 5 7 ....
// red id 2 4 6 8 10 ....
// reset all no color
// 練習二
// first => first red other 沒顏色
// last => last red other 沒顏色
// 練習三
// 當我按學生姓名
// tr red
// next tr blue
// prev tr green
function resetFun() {
myTable.find('tr').css('color', 'black');
}
//btn
const firstBtn = $('.firstBtn');
const lastBtn = $('.lastBtn');
const greenBtn = $('.green');
const redBtn = $('.red');
const resetBtn = $('.reset');
const myTable = $('#myTable');
// table childrend 非tr
// table find('tr')
const myTr = $('tr');
console.log(myTable.find('tr'));
greenBtn.click(function () {
resetFun();
console.log('greenBtn ok');
console.log('children', table.children());
// myTr.odd().css('color', 'green');
myTable.find('tr').odd().css('color', 'green');
});
redBtn.click(function () {
resetFun();
console.log('redBtn ok');
// myTr.evne().css('color', 'red');
myTable.find('tr').even().not('tr:first').css('color', 'red');
});
resetBtn.click(function () {
resetFun();
console.log('resetBtn ok');
});
firstBtn.click(function () {
resetFun();
myTable.find('td').parent().first().next().next().css('color', 'red');
});
lastBtn.click(function () {
resetFun();
myTable.find('tr').last().css('color', 'red');
});
// 方法一 class
$('.myName').each(function (index,element) {
$(this).click(function () {
console.log(index);
let getID = $(this).parent().prev().text();
let getName = $(this).text();
let getMobile = $(this).parent().next().text();
resetFun();
log($(this).parent().parent().prev());
$(this).parent().parent().prev().css('color','green');
$(this).parent().parent().css('color','red');
$(this).parent().parent().next().css('color','blue');
alert(`Hello ${getID} / ${getName} / ${getMobile} `);
});
});
});
</script>
</body>
</html>