0
我想要創建tabindex的元素,並通過按鍵向上導航我想獲得該元素的索引,傳遞給計數器並將下一個元素設置爲活動。問題是我無法獲得所關注元素的索引。代碼:空間導航
var focusable = Array.from(document.querySelectorAll('[tabindex]'));
focusable[1].focus()
window.addEventListener('keydown', function(e) {
var counter;
if (e.keyCode == '38') {
//here i want to pass the index of elment
//from array which is focused and pass it to counter
console.log('up');
counter++
console.log(counter)
focusable[counter].focus()
}
});