How can i catch the firefox spellcheck correction event?

北城余情 提交于 2019-12-04 03:31:29

问题


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.


回答1:


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/




回答2:


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.



来源:https://stackoverflow.com/questions/9077624/how-can-i-catch-the-firefox-spellcheck-correction-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!