File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed
team/Etrex/Day10 - multi check Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -165,7 +165,7 @@ <h5 class="font-weight-bold font-italic text-white team-tag">TEAM</h5>
165165 < li > Day#09|< span > Dev Tools Domination</ span >
166166 </ li >
167167 </ a >
168- < a class ="project-item " href ="# ">
168+ < a class ="project-item " href ="team/Etrex/Day10 - multi check/index.html ">
169169 < li > Day#10|< span > Hold Shift and Check Checkboxes</ span >
170170 </ li >
171171 </ a >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Document</ title >
7+ </ head >
8+ < body >
9+
10+ </ body >
11+ < script >
12+ // 生成一堆 checkbox
13+ const a = [ ]
14+ a . length = 1000 ;
15+ a . fill ( '<input type="checkbox">' )
16+ document . querySelector ( 'body' ) . innerHTML = a . join ( '\n' )
17+ const checkboxes = [ ...document . querySelectorAll ( 'input[type="checkbox"]' ) ] ;
18+
19+ let lastCheckIndex ;
20+ checkboxes . forEach ( checkbox => checkbox . addEventListener ( 'click' , function ( event ) {
21+ if ( event . shiftKey ) {
22+ // 清空 lastCheckIndex 上下有選到的 checkbox
23+ for ( let i = lastCheckIndex - 1 ; i >= 0 ; i -- ) {
24+ if ( checkboxes [ i ] . checked ) {
25+ checkboxes [ i ] . checked = false
26+ } else {
27+ break ;
28+ }
29+ }
30+ for ( let i = lastCheckIndex + 1 ; i < checkboxes . length ; i ++ ) {
31+ if ( checkboxes [ i ] . checked ) {
32+ checkboxes [ i ] . checked = false
33+ } else {
34+ break ;
35+ }
36+ }
37+ // 選取從 lastCheckIndex 到目前點擊的 checkbox
38+ const currentCheckIndex = checkboxes . indexOf ( event . target )
39+ const index1 = Math . min ( lastCheckIndex , currentCheckIndex )
40+ const index2 = Math . max ( lastCheckIndex , currentCheckIndex )
41+ for ( let i = index1 ; i <= index2 ; i ++ ) {
42+ checkboxes [ i ] . checked = true
43+ }
44+ } else {
45+ lastCheckIndex = checkboxes . indexOf ( event . target )
46+ }
47+ } ) )
48+ </ script >
49+ </ html >
You can’t perform that action at this time.
0 commit comments