Skip to content

Commit 7579288

Browse files
committed
Fix focus in elements that are not focused, using tabindex -1
1 parent 2d400ff commit 7579288

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/skip-to.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,35 @@ export default {
1919
type: [Number, null],
2020
default: null
2121
}
22+
},
23+
24+
mounted () {
25+
this.init()
26+
},
27+
28+
methods: {
29+
init () {
30+
window.addEventListener('hashchange', () => {
31+
this.focusElement(location.hash.substring(1))
32+
}, false)
33+
34+
if (location.hash && location.hash.substring(1)) {
35+
console.log(location.hash.substring(1))
36+
this.focusElement(location.hash.substring(1))
37+
}
38+
},
39+
40+
focusElement (id) {
41+
if (!id) return
42+
let element = window.document.getElementById(id)
43+
if (element) {
44+
if (!/^(a|select|input|button|textarea)/i.test(element.tagName.toLowerCase())) {
45+
console.log('entrou')
46+
element.setAttribute('tabindex', -1)
47+
}
48+
element.focus()
49+
}
50+
}
2251
}
2352
}
2453
</script>

0 commit comments

Comments
 (0)