我如何捕获 Firefox 拼写检查更正事件?

发布于 2025-01-01 03:34:30 字数 187 浏览 0 评论 0原文

我有一个文本区域。写下一些拼写错误的文本并使用右键单击 -> 后更正 该单词将被替换为拼写正确的单词。 现在,我的问题是,修正完成后我需要执行一些 javascript 代码。

如何捕获 Firefox 拼写检查更正事件? 如果只有一种使用 Firefox 附加组件的解决方案,我也很高兴知道这一点。

I have a textarea. After writing some misspelled text and using rightclick -> correction the word gets replaced with a correctly spelled word.
Now, my problem here is that i need to exectue some javascript code when the correction gets done.

How can i catch the firefox spellcheck correction event?
If there is a only a solution using a firefox Add-On i would be happy too to know that one.

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

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

发布评论

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

评论(2

原谅过去的我 2025-01-08 03:34:30

在这种情况下,Mozilla 会触发 oninput ,没有在其他地方进行测试,但应该可以在任何地方工作。

有趣的是,FF 在使用拼写校正时似乎会触发两个输入事件:它首先删除单词,然后插入新单词:

> value=[holy coww]
(right click and choose "cow")
> value=[holy ]
> value=[holy cow]

Mozilla fires oninput in this case, didn't test in others, but should work everywhere.

Interestingly enough, FF seems to fire two input events when using spelling correction: it deletes the word first, and then inserts the new one:

> value=[holy coww]
(right click and choose "cow")
> value=[holy ]
> value=[holy cow]

http://jsfiddle.net/7ssYq/

亣腦蒛氧 2025-01-08 03:34:30

我原本打算建议 oninput 事件,例如 thg435 的答案,但我想我'首先在评论中寻找更多详细信息。如果您不需要区分拼写检查器更正和其他类型的输入(键盘、粘贴、拖放等),那么 oninput 就可以很好地完成这项工作。

如果您确实想要区分这些类型的输入,那么恐怕没有专门为拼写检查器更正而触发的事件。但是,大多数其他类型的输入都有事件,因此,如果您首先检查其他类型的事件,至少可以缩小输入事件为更正的可能性。请考虑以下事项:

(function () {
    var el = document.getElementById("MyInput"),
        ignore = false;

    el.oninput = function (e) {
        // ignore the events that we don't need to capture
        if (ignore) {
            ignore = false;
            return true;
        }

        // Your code here
    }

    // IIRC, you need the following line for the `ondrop` event to fire
    el.ondragover = function () { return false; }

    // Ignore paste, drop and keypress operations
    el.onpaste = el.ondrop = el.onkeypress = setIgnore;

    function setIgnore (e) {
        ignore = true; 
    }
})();

然而,这并不是一个完美的解决方案。例如,对于不是由键盘启动的撤消/重做操作(可能还有其他一些操作),该事件仍然会触发。

I was originally going to suggest the oninput event, like thg435's answer, but I thought I'd fish for more details in the comments first. If you don't need to differentiate between spell checker corrections and other types of input (keyboard, paste, drag and drop, etc), then oninput would do the job just fine.

If you do want to differentiate between those types of input, then I'm afraid there's no event that fires specifically for spell checker corrections. However, there are events for most other types of input, so you could at least narrow down the likelihood of your input event being a correction if you check for other types of event first. Consider the following:

(function () {
    var el = document.getElementById("MyInput"),
        ignore = false;

    el.oninput = function (e) {
        // ignore the events that we don't need to capture
        if (ignore) {
            ignore = false;
            return true;
        }

        // Your code here
    }

    // IIRC, you need the following line for the `ondrop` event to fire
    el.ondragover = function () { return false; }

    // Ignore paste, drop and keypress operations
    el.onpaste = el.ondrop = el.onkeypress = setIgnore;

    function setIgnore (e) {
        ignore = true; 
    }
})();

This isn't a perfect solution, however. For instance, the event will still fire for Undo/Redo actions (and, perhaps some other actions) that aren't initiated by the keyboard.

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