-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.html
More file actions
51 lines (41 loc) · 1.33 KB
/
a.html
File metadata and controls
51 lines (41 loc) · 1.33 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
<!-- code by OpenAI, Q: move focus on link by arrow key press -->
<!-- keep disclamer: You may want to modify this code to fit your specific needs, but this should give you a starting point for moving focus on arrow key press. -->
<a href="#" tabindex="1">Link 1</a>
<a href="#" tabindex="2">Link 2</a>
<a href="#" tabindex="3">Link 3</a>
<script>
// Get all of the links in the group
var links = document.querySelectorAll('a[tabindex]');
document.addEventListener('keydown', function(event) {
// Move focus to the previous element in the group
if (event.key === 'ArrowLeft') {
// Find the currently focused link
var currentIndex = -1;
for (var i = 0; i < links.length; i++) {
if (links[i] === document.activeElement) {
currentIndex = i;
break;
}
}
// If there is a currently focused link, move focus to the previous link
if (0 < currentIndex) {
links[currentIndex - 1].focus();
}
}
// Move focus to the next element in the group
if (event.key === 'ArrowRight') {
// Find the currently focused link
var currentIndex = -1;
for (var i = 0; i < links.length; i++) {
if (links[i] === document.activeElement) {
currentIndex = i;
break;
}
}
// If there is a currently focused link, move focus to the next link
if (-1 < currentIndex && currentIndex < links.length - 1) {
links[currentIndex + 1].focus();
}
}
});
</script>