用于输入中文的 JQuery/DOM 事件(ibus)?
我们知道,当我们打字时,会触发很多事件。
例如 keyup、keydown、keypress 或其他。
是否有其他事件只有在文本字段中的内容更改时才会触发?如果没有,如何编写一些 javasrcipt 代码来完成此功能
as we know there is a lot of events will be triggered when we typing.
such as keyup, keydown, keypress or something else.
Is there any other event will be triggered only when the content in the text field is changed? and if there is not,how to write some javasrcipt code to accomplish this feature
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
change 事件可能有帮助吗?
The change event might help?
我创建了一个简单的小提琴来确定当通过按键修改 contenteditable 元素中的文本节点时会发生什么。当我这样做时,我决定检查使用 ibus 时会发生什么,因为我将 ibus 用于我自己的工作。我确定,当使用 ibus 时,生成的唯一键盘事件是带有
which
值的 keyup 事件,该值是无用的。这是我创建的小提琴:
http://jsfiddle.net/lddubeau/jWnL3/
转到那个小提琴。
打开您的 Javascript 控制台。
单击运行。
在单词“toto”的第 2dn 和第三个字符之间单击。
使用 ibus 输入一些内容。例如,键入“我”。
输出因您的浏览器而异。在 Chrome 29 上,每个击键都会被记录,因此如果我使用tonepy 方法输入“我”,我会得到每个“w”“o”“3”和“1”的 keyup 和 keydown。 (拼音,声调,然后按1选择第一个选项。)最终的事件是:
在此事件之前的事件都具有相同的值,只是有些事件是“keydown”类型,当然节点值显示“toto”,直到最后一个事件。
在 Firefox 23 上仅注册一个事件:
不生成任何按键或按键事件。 (当输入 ASCII 范围内的字符时,您会得到每个键的 keydown、keypress 和 keyup。)
which 和 keyCode 的值似乎与任何合理的值都不对应。我检查了传递给事件处理程序的事件对象,没有看到任何表明用户刚刚键入“我”的字段。
I've created a simple fiddle to determine what happens when a text node in a contenteditable element is modified by a key press. As I was doing that, I decided to check what happens when using ibus because I use ibus for my own work. I determined that when ibus is used, the only keyboard event generated is a keyup event with a
which
value which is useless.Here is the fiddle I created:
http://jsfiddle.net/lddubeau/jWnL3/
Go to that fiddle.
Turn on your Javascript console.
Click run.
Click between the 2dn and third character of the word "toto".
Type something using ibus. For instance, type 我.
The output varies depending on your browser. On Chrome 29 each keystroke is recorded so if I type 我 using the tonepy method I get keyup and keydown for each of the "w" "o" "3" and "1". (The pinyin, the tone and then hitting 1 to select the first choice.) The final event is:
The events before this one all have the same values except that some are of the "keydown" type and of course the node value shows "toto" until the last event.
On Firefox 23 only one event is registered:
No keydown or keypress events are generated. (When typing characters within the ascii range, you get keydown, keypress and keyup for each key.)
The values of which and keyCode do not seem to correspond to anything sensible. I've examined the event object passed to the event handler and did not see any field which would indicate that the user just typed 我.
如果您处理中文输入并且对输入拼音等时触发
change
事件的中间输入不感兴趣,那么您可能需要查看compositionstart
和compositionend< /代码> 事件。这些在合成的开始和结束时触发。
即使是拉丁字符也可以触发构图。例如,在 iOS 上,当编写 ` 时,反引号字符会显示下划线,表示需要第二个字符才能完成输入。选择 e 会触发
compositionend
事件,并将event.data
设置为è
。键盘事件(例如
keydown
和keyup
)上还有一个isCompositing
属性,用于指示输入是否是组合的一部分。请注意,您可能必须组合不同的事件才能获得所需的效果。也许通过使用
compositionstart
和compositionend
来跟踪组合是否正在发生并忽略组合内触发的更改事件。If you handle Chinese input and are not interested in intermediate input which triggers
change
events while entering Pinyin etc. then you may want to look into thecompositionstart
andcompositionend
events. These are fired at the beginning and end of composition.Even latin characters can trigger a composition. For example on iOS when writing `, the backtick character is displayed underlined, indicating that a second character is needed to complete the entry. Selecting e triggers a
compositionend
event withevent.data
set toè
.There is also a
isComposing
attribute on keyboard events such askeydown
andkeyup
that indicates whether the input is part of a composition or not.Note that you may have to combine different events to get the effect that you want. Perhaps by using
compositionstart
andcompositionend
to keep track of whether a composition is happening and ignoring change events that are triggered within compositions.