使用表中的箭头键,但要从选定的表单元格开始
我正在尝试使用箭头键来导航表。
以下代码有效,但问题在于它将仅从最后一个位置导航,使用了箭头键。如果我单击表中其他地方的单元格,则箭头键是从键盘移动的最后一个位置开始的,而不是单击的单元格。 这是代码,我假设我需要将开始从查询选择器更改为鼠标上次单击/选择的任何地方。 有什么想法吗?只请javascript,没有jQuery。 谢谢
function Mouse() {
let start = document.querySelector('.first-element');
const changeStyle = (sibling) => {
if (sibling !== null) {
start.focus();
sibling.focus();
start = sibling;
}
}
const checkKey = (event) => {
event = event || window.event;
const idx = start.cellIndex;
if (event.keyCode === 38) {
// up arrow
const previousRow = start.parentElement.previousElementSibling;
if (previousRow !== null) {
const previousSibling = previousRow.cells[idx];
changeStyle(previousSibling);
}
} else if (event.keyCode === 40) {
// down arrow
const nextRow = start.parentElement.nextElementSibling;
if (nextRow !== null) {
const nextSibling = nextRow.cells[idx];
changeStyle(nextSibling);
}
} else if (event.keyCode === 37) {
// left arrow
const previousSibling = start.previousElementSibling;
changeStyle(previousSibling);
} else if (event.keyCode === 39) {
// right arrow
const nextsibling = start.nextElementSibling;
changeStyle(nextsibling);
}
}
document.onkeydown = checkKey;
}
I am trying to use arrow keys to navigate a table.
The below code works but the problem is that it will only navigate from the last place the arrow key was used. If I click on a cell somewhere else in the table, the arrow key starts from the last position that was moved with the keyboard, not the clicked cell.
This is the code and I assume I need to change the start from the query selector to wherever the mouse last clicked/selected.
Any ideas? Javascript only please, no jquery.
Thank you
function Mouse() {
let start = document.querySelector('.first-element');
const changeStyle = (sibling) => {
if (sibling !== null) {
start.focus();
sibling.focus();
start = sibling;
}
}
const checkKey = (event) => {
event = event || window.event;
const idx = start.cellIndex;
if (event.keyCode === 38) {
// up arrow
const previousRow = start.parentElement.previousElementSibling;
if (previousRow !== null) {
const previousSibling = previousRow.cells[idx];
changeStyle(previousSibling);
}
} else if (event.keyCode === 40) {
// down arrow
const nextRow = start.parentElement.nextElementSibling;
if (nextRow !== null) {
const nextSibling = nextRow.cells[idx];
changeStyle(nextSibling);
}
} else if (event.keyCode === 37) {
// left arrow
const previousSibling = start.previousElementSibling;
changeStyle(previousSibling);
} else if (event.keyCode === 39) {
// right arrow
const nextsibling = start.nextElementSibling;
changeStyle(nextsibling);
}
}
document.onkeydown = checkKey;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这会为您提供帮助。
移动密钥是[alt] + [shift] + [箭头键]。
I believe that it will help you.
move key is [alt] + [shift] + [arrow keys].