Skip to content

Commit e549dbe

Browse files
authored
Merge pull request #45 from lizwang50/etrex-day10
Etrex day10
2 parents 0541a86 + c7ee0d7 commit e549dbe

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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>

0 commit comments

Comments
 (0)