有没有办法使用JavaScript或CSS关闭Chrome中文本区域的拼写检查?

Cha*_* Wu 11 javascript google-chrome

我遇到了这个问题.我有一个textarea,我只想在它聚焦时使用拼写检查.

<textarea id="editor"></textarea>

$('#editor').focusin(function(){
    $(this).attr('spellcheck', true);
});

$('#editor').focusout(function(){
    $(this).attr('spellcheck', false);
});
Run Code Online (Sandbox Code Playgroud)

在chrome中,如果单词拼写错误,则单词下面会出现一条红线.即使我关闭拼写检查,红线仍然存在.如何删除此标记?

Cha*_* Wu 2

明白了

function bindEditorFocus() {
    var $editor = $('#editor');
    $editor.focusin(function(){
        $(this).attr('spellcheck', true);
        toggleSpellingcheck(); // loop through all words to add marker
    }); 
    
    $editorblur(function(){
        $editor.attr('spellcheck', false);
        $editor.unbind();    // I need to unbind all function to avoid a loop 
        toogleSpellingcheck(); // loop through all words to remove marker
        $editor.blur();     //get out of text area
        bindEditorFocus();  // rebind all functions 
    });
}


function toogleSpellingcheck(){ 
    //this will loop through all words
    var $editor = $('#editor'); 
    var text = $editor.val();       
    for (var i = 0; i < text.length; i++) {
        $editor.caret({start:i,end:i});
    }
}
Run Code Online (Sandbox Code Playgroud)

ToogleSpellingcheck 方法循环遍历所有单词,它可以优化为循环遍历单词而不是字符,但这需要 jquery caret 插件

虽然有点乱,但是可以用,任何人有改进建议请告诉我


归档时间:

查看次数:

4180 次

最近记录:

13 年,2 月 前