如何忽略 Javascript 中的 keyEvent?

发布于 2024-10-09 01:43:05 字数 819 浏览 3 评论 0原文

我在 YUI 中有一个数据表。我试图让表忽略所有关键事件。我已经尝试过这些方法:

YAHOO.util.Event.addListener(singleSelectDataTable, "keydown", function(oEvent) {
    YAHOO.util.Event.stopPropagation(oEvent); 
});

YAHOO.util.Event.preventDefault(singleSelectDataTable.tableKeyEvent);    

singleSelectDataTable.subscribe('tableKeyEvent', function(oArgs) { 
  YAHOO.util.Event.preventDefault(oArgs.event); 
});

我已经查看了几个 YUI 示例来拦截单击事件,但它们并不能类比到这个特定场景。我创建了一个独立的 HTML 测试文件(如果有帮助):http://pastebin.com/khfR4Stk。根本问题是我们不想在表格中支持向上箭头键或向下箭头键;它是一个滚动表,为了使其正常工作,一旦选择超出“显示窗口”,我们就必须调整滚动滑块。

我能想到的唯一其他解决方案是订阅 tableKeyEvent ,然后如果按键是向上箭头,则取消选择新选择的行,选择上一行,对向下箭头执行适当的模拟(基本上撤消了按键刚刚完成)。这似乎不是正确的解决方案......

I have a DataTable in YUI. I'm trying to get the table to ignore all keyEvents. I've tried these methods:

YAHOO.util.Event.addListener(singleSelectDataTable, "keydown", function(oEvent) {
    YAHOO.util.Event.stopPropagation(oEvent); 
});

OR

YAHOO.util.Event.preventDefault(singleSelectDataTable.tableKeyEvent);    

OR

singleSelectDataTable.subscribe('tableKeyEvent', function(oArgs) { 
  YAHOO.util.Event.preventDefault(oArgs.event); 
});

I've looked at a couple of YUI examples to intercept click events, but they don't analogize to this specific scenario. I created a standalone HTML test file if that will help: http://pastebin.com/khfR4Stk. The foundational problem is that we don't want to support arrow key up or arrow key down in our tables; it's a scrolling table and in order for it to work properly we would have to adjust the scrolling thumb once the selection goes past the 'shown-window'.

The only other solution I could think of is to subscribe to the tableKeyEvent and then if the keypress is up-arrow, then unselect the newly selected row, selecting the previous row, doing the appropriate analogue for a down-arrow (basically undoing what the keypress just did). This didn't seem like the right solution…

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与风相奔跑 2024-10-16 01:43:05

tableKeyEvent 在处理向上/向下箭头键后引发。因此,试图阻止该事件是无济于事的。

查看 DataTable 小部件的 _onTbodyKeydown 函数,我注意到将选择模式设置为无效模式会禁用方向键导航。幸运的是,它似乎并没有破坏其他选择处理。至少在你的例子中不是。

因此,只需将 selectionMode:"single" 更改为 SelectionMode:"" 就可以了:-)
当然不能保证这在未来版本中有效

The tableKeyEvent is raised after the up/down arrow key has been handled. So trying to stop that event will not help.

Looking at the _onTbodyKeydown function of the DataTable widget, I noticed that setting the selection mode to an invalid mode disables key arrow key navigation. Luckily it doesn't seem to break the other selection handling. At least not in your example.

So just change selectionMode:"single" to selectionMode:"" and you should be fine :-)
(Of course there is no guarantee that this will work in future versions)

夜吻♂芭芘 2024-10-16 01:43:05

尝试创建此 onclick 的等效

function noenter(evt)
{
    var k = evt.keyCode||evt.which;
    return k != 13;
}

获取 getCharCode 的语法,并告诉脚本当它接收输入时,它需要否认它。

Try creating the equiv of this onclick

function noenter(evt)
{
    var k = evt.keyCode||evt.which;
    return k != 13;
}

Get the syntax for getCharCode and tell the script that when it receives input, it needs to deny it.

也只是曾经 2024-10-16 01:43:05

难道你不能只添加一个向 keydown 事件返回 false 的事件处理程序吗?

can't you just add an eventhandler that returns false to the keydown event?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文