gpt4 book ai didi

javascript - 仅在 Firefox (JQuery) 文本字段的开始和结束时不触发按键事件

转载 作者:行者123 更新时间:2023-11-30 06:49:13 26 4
gpt4 key购买 nike

我正在使用委托(delegate)监听输入字段上的按键事件。出于某种原因,Firefox 不会在字段开始时触发光标向上或在结束时光标向下的委托(delegate)事件。 LEFT 和 RIGHT 始终按预期工作。

直接将事件监听器绑定(bind)到该字段工作正常,因此它必须与委托(delegate)有关。有谁知道这是否是一个已知问题,我在谷歌/论坛等上找不到任何东西......?

$("div").delegate(":input", "keypress", function(e){
// doesn't get triggered
});

$("div :input").bind("keypress", function(e){
// gets triggered fine
});

这是一个显示问题的演示 - http://livsey.org/jquery.delegation.html

最佳答案

这些键在 Firefox 中不会冒泡,至少在那种情况下不会,所以 .delegate().live() 将不起作用。这是一个已知问题,在这种情况下最好使用不同的事件,例如 keydownkeyup,您可以看到 jQuery documentation for .keypress()快速介绍一下:

Note that keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A" is reported as 65 by all events. Because of this distinction, when catching special keystrokes such as arrow keys, .keydown() or .keyup() is a better choice.

更新您的代码以使其有效:

$("div")().delegate(":input", "keyup", function(e){
log("delegated: "+e.keyCode);
});

$("div :input").bind("keyup", function(e){
log("bound: "+e.keyCode);
});

关于javascript - 仅在 Firefox (JQuery) 文本字段的开始和结束时不触发按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2777148/

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