File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
team/Etrex/Day10 - multi check Expand file tree Collapse file tree 1 file changed +50
-0
lines changed 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+
20+ let lastCheckIndex ;
21+ checkboxes . forEach ( checkbox => checkbox . addEventListener ( 'click' , function ( event ) {
22+ if ( event . shiftKey ) {
23+ // 清空 lastCheckIndex 上下有選到的 checkbox
24+ for ( let i = lastCheckIndex - 1 ; i >= 0 ; i -- ) {
25+ if ( checkboxes [ i ] . checked ) {
26+ checkboxes [ i ] . checked = false
27+ } else {
28+ break ;
29+ }
30+ }
31+ for ( let i = lastCheckIndex + 1 ; i < checkboxes . length ; i ++ ) {
32+ if ( checkboxes [ i ] . checked ) {
33+ checkboxes [ i ] . checked = false
34+ } else {
35+ break ;
36+ }
37+ }
38+ // 選取從 lastCheckIndex 到目前點擊的 checkbox
39+ const currentCheckIndex = checkboxes . indexOf ( event . target )
40+ const index1 = Math . min ( lastCheckIndex , currentCheckIndex )
41+ const index2 = Math . max ( lastCheckIndex , currentCheckIndex )
42+ for ( let i = index1 ; i <= index2 ; i ++ ) {
43+ checkboxes [ i ] . checked = true
44+ }
45+ } else {
46+ lastCheckIndex = checkboxes . indexOf ( event . target )
47+ }
48+ } ) )
49+ </ script >
50+ </ html >
You can’t perform that action at this time.
0 commit comments