使用箭头键进行导航
我想知道是否可以使用箭头键浏览我用 JS(使用 jQuery)创建的表?我的意思是从一个单元格跳到另一个单元格......该脚本是为 Greasemonkey 设计的。
然而,警报有效。我只是不知道如何让它运转良好。
$(document).keydown(function(e){
if (e.keyCode == 37) {
alert( "left pressed " );
return false;
}
if (e.keyCode == 38) {
alert( "up pressed " );
return false;
}
if (e.keyCode == 39) {
alert( "right pressed " );
return false;
}
if (e.keyCode == 40) {
alert( "down pressed " );
return false;
}
});
;
任何提示或任何东西都非常感激。 提前致谢, Faili
更新
看来我对自己的解释是错误的。再给我一次尝试: 演示
这个可能会让您了解我想要什么。选择一个输入字段后,可以使用箭头键进行导航。 我的问题是我无法通过 GM 和 jQuery 实现它。有什么想法吗?
再次感谢您的时间和努力。
最后就像:
function myTest_analysis1(e,leftkey,up,right,down){
myTest(e,'','','field_analysis2','field_communication1')
function myTest(e,leftkey,up,right,down)
{
if (!e) e=window.event;
var selectArrowKey;
switch(e.keyCode)
{
case 37:
// Key left.
selectArrowKey = leftkey;
break;
case 38:
// Key up.
selectArrowKey = up;
break;
case 39:
// Key right.
selectArrowKey = right;
break;
case 40:
// Key down.
selectArrowKey = down;
break;
}
if (!selectArrowKey) return;
var controls = window.document.getElementById(selectArrowKey);
if (!controls) return;
controls.focus();
}
}
$('#field_analysis1').keydown (myTest_analysis1);
这就是我的结果。我敢打赌有一个更聪明的解决方案,但我现在无法弄清楚。
非常感谢您的时间和精力。
I am wondering if there was a possibility to navigate with arrow keys through a table I created with JS(using jQuery)? I mean jumping from cell to cell...The script is for Greasemonkey.
The alert, however, works. I just got no idea how to make it well-functioning.
$(document).keydown(function(e){
if (e.keyCode == 37) {
alert( "left pressed " );
return false;
}
if (e.keyCode == 38) {
alert( "up pressed " );
return false;
}
if (e.keyCode == 39) {
alert( "right pressed " );
return false;
}
if (e.keyCode == 40) {
alert( "down pressed " );
return false;
}
});
;
Any hint or whatever is much appreciated.
Thanks in advance,
Faili
Update
It seems like I explained myself wrong. Give me another try:
Demo
This one may give you an idea of what I wanted. After selecting one input-field a navigation with arrow keys is possible.
My problem is that I just can't realise it via GM and jQuery. Any idea?
Thanks again for your time and effort.
Finally it was like:
function myTest_analysis1(e,leftkey,up,right,down){
myTest(e,'','','field_analysis2','field_communication1')
function myTest(e,leftkey,up,right,down)
{
if (!e) e=window.event;
var selectArrowKey;
switch(e.keyCode)
{
case 37:
// Key left.
selectArrowKey = leftkey;
break;
case 38:
// Key up.
selectArrowKey = up;
break;
case 39:
// Key right.
selectArrowKey = right;
break;
case 40:
// Key down.
selectArrowKey = down;
break;
}
if (!selectArrowKey) return;
var controls = window.document.getElementById(selectArrowKey);
if (!controls) return;
controls.focus();
}
}
$('#field_analysis1').keydown (myTest_analysis1);
That's how it worked out for me. I bet there is a smarter solution, but I couldn't figure it out right now.
Thank you sooo much for your time and effort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个版本,允许
演示位于:https://jsfiddle.net /BdVB9/
具有类似 html 结构
和 javascript
Here is a version that allows for the following
Demo at : https://jsfiddle.net/BdVB9/
with an html structure like
and javascript
您应该能够聚焦各个单元格,我将使用 .focus() 将一个示例放在一起,
这是示例。
请记住...
a) 此示例中没有任何内容可以阻止您超出范围,您需要将 currentRow 和 currentCell 的值限制为单元格数量并防止它们低于 0。
b )目前,没有代码可以删除绿色文本,这是我用来显示当前焦点的文本。这意味着留下了一条绿色小径。
您可以相当轻松地解决上述两个问题,但它们会使示例变得更加复杂......
You should be able to focus the various cells, I will put an example together using .focus()
Here is the example.
Please bear in mind that...
a) There is nothing in this example to stop you from going out of bounds, you would need to restrict the values of currentRow and currentCell to the number of cells and prevent them from going below 0.
b) At the moment, there is no code to remove the green text, which is what I've used to show the current focus. This means a green trail is left behind.
You could solve both of the above fairly easily, but they would make the example more complicated...
这是我的版本...
演示
here is my version...
demo