gpt4 book ai didi

javascript - 模式中的 Vanilla javascript Trap Focus(可访问性选项卡)

转载 作者:行者123 更新时间:2023-11-30 16:33:37 24 4
gpt4 key购买 nike

这应该很简单,但由于某种原因它不起作用,我在正确的时间获得了正确的 console.logs,但焦点没有转到正确的位置,请引用我的 jsfiddle

https://jsfiddle.net/bqt0np9d/

function checkTabPress(e) {
"use strict";
// pick passed event of global event object
e = e || event;

if (e.keyCode === 9) {
if (e.shiftKey) {
console.log('back tab pressed');
firstItem.onblur=function(){
console.log('last a focus left');
lastItem.focus();
};
e.preventDefault();
}
console.log('tab pressed');
lastItem.onblur=function(){
console.log('last a focus left');
firstItem.focus();
};
e.preventDefault();
}
}
modal.addEventListener('keyup', checkTabPress);

最佳答案

我必须将焦点锁定在我们在 React 组件中使用的模态中。我为 KEY DOWN 添加了 eventListner 并收集了 Tab 和 Shift+Tab

class Modal extends Component {
componentDidMount() {
window.addEventListener("keyup", this.handleKeyUp, false);
window.addEventListener("keydown", this.handleKeyDown, false);
}

componentWillUnmount() {
window.removeEventListener("keyup", this.handleKeyUp, false);
window.removeEventListener("keydown", this.handleKeyDown, false);
}

handleKeyDown = (e) => {

//Fetch node list from which required elements could be grabbed as needed.
const modal = document.getElementById("modal_parent");
const tags = [...modal.querySelectorAll('select, input, textarea, button, a, li')].filter(e1 => window.getComputedStyle(e1).getPropertyValue('display') === 'block');
const focusable = modal.querySelectorAll('button, [href], input, select, textarea, li, a,[tabindex]:not([tabindex="-1"])');
const firstFocusable = focusable[0];
const lastFocusable = focusable[focusable.length - 1];

if (e.ctrlKey || e.altKey) {
return;
}

const keys = {
9: () => { //9 = TAB
if (e.shiftKey && e.target === firstFocusable) {
lastFocusable.focus();
}

if (e.target === lastFocusable) {
firstFocusable.focus();
}
}
};

if (keys[e.keyCode]) {
keys[e.keyCode]();
}
}
}

关于javascript - 模式中的 Vanilla javascript Trap Focus(可访问性选项卡),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com