用于输入中文的 JQuery/DOM 事件(ibus)?

发布于 2024-12-02 06:36:11 字数 132 浏览 1 评论 0原文

我们知道,当我们打字时,会触发很多事件。

例如 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 技术交流群。

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

发布评论

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

评论(3

秋千易 2024-12-09 06:36:11

change 事件可能有帮助吗?

The change event might help?

甜警司 2024-12-09 06:36:11

我创建了一个简单的小提琴来确定当通过按键修改 contenteditable 元素中的文本节点时会发生什么。当我这样做时,我决定检查使用 ibus 时会发生什么,因为我将 ibus 用于我自己的工作。我确定,当使用 ibus 时,生成的唯一键盘事件是带有 which 值的 keyup 事件,该值是无用的。

这是我创建的小提琴:

http://jsfiddle.net/lddubeau/jWnL3/

  1. 转到那个小提琴。

  2. 打开您的 Javascript 控制台。

  3. 单击运行。

  4. 在单词“toto”的第 2dn 和第三个字符之间单击。

  5. 使用 ibus 输入一些内容。例如,键入“我”。

  6. 输出因您的浏览器而异。在 Chrome 29 上,每个击键都会被记录,因此如果我使用tonepy 方法输入“我”,我会得到每个“w”“o”“3”和“1”的 keyup 和 keydown。 (拼音,声调,然后按1选择第一个选项。)最终的事件是:

    事件类型:keyup
    其中:229
    密钥代码:229
    字符代码:0
    节点值: 
       到我到
    

    在此事件之前的事件都具有相同的值,只是有些事件是“keydown”类型,当然节点值显示“toto”,直到最后一个事件。

    在 Firefox 23 上仅注册一个事件:

    “事件类型:”“keyup”
    “其中:”49
    “密钥代码:”49
    “字符代码:”0
    “节点值:”“
        到我到
    ”
    

    不生成任何按键或按键事件。 (当输入 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/

  1. Go to that fiddle.

  2. Turn on your Javascript console.

  3. Click run.

  4. Click between the 2dn and third character of the word "toto".

  5. Type something using ibus. For instance, type 我.

  6. 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:

    event type: keyup
    which: 229
    keyCode: 229
    charCode: 0
    node value: 
       to我to
    

    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:

    "event type:" "keyup"
    "which:" 49
    "keyCode:" 49
    "charCode:" 0
    "node value:" "
        to我to
    "
    

    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 我.

笑咖 2024-12-09 06:36:11

如果您处理中文输入并且对输入拼音等时触发 change 事件的中间输入不感兴趣,那么您可能需要查看 compositionstartcompositionend< /代码> 事件。这些在合成的开始和结束时触发。

即使是拉丁字符也可以触发构图。例如,在 iOS 上,当编写 ` 时,反引号字符会显示下划线,表示需要第二个字符才能完成输入。选择 e 会触发 compositionend 事件,并将 event.data 设置为 è

键盘事件(例如 keydownkeyup)上还有一个 isCompositing 属性,用于指示输入是否是组合的一部分。

请注意,您可能必须组合不同的事件才能获得所需的效果。也许通过使用compositionstartcompositionend来跟踪组合是否正在发生并忽略组合内触发的更改事件。

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 the compositionstart and compositionend 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 with event.data set to è.

There is also a isComposing attribute on keyboard events such as keydown and keyup 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 and compositionend to keep track of whether a composition is happening and ignoring change events that are triggered within compositions.

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