【问题标题】:Is there a way to turn off spell check in a textarea? [duplicate]有没有办法在文本区域中关闭拼写检查? [复制]
【发布时间】:2012-06-20 02:31:09
【问题描述】:

我遇到了这个问题。我有一个文本区域,我只想在它处于焦点时使用拼写检查。

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

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

$('#editor').focusout(function(){
    $(this).attr('spellcheck', false);
});

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

【问题讨论】:

  • 对于 SuperUser 来说,这听起来更像是一个问题,而不是 Stack Overflow。
  • 这是一个编程问题。我需要在代码中这样做
  • 重新创建一个包含相同文本的文本区域,但在将其放入 DOM 之前,请先将 spellcheck 设为 false。

标签: javascript html google-chrome


【解决方案1】:

想通了

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});
    }
}

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

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

【讨论】:

    【解决方案2】:

    虽然在

    ????:

    <textarea id="my-ta" spellcheck="whatever">abcd dcba</textarea>
    

    ??????????:

    function setSpellCheck( mode ) {
        var myTextArea = document.getElementById( "my-ta" )
            , myTextAreaValue = myTextArea.value
        ;
        myTextArea.value = '';
        myTextArea.setAttribute( "spellcheck", String( mode ) );
        myTextArea.value = myTextAreaValue;
        myTextArea.focus();
    }
    

    ?????:

    setSpellCheck( true );
    setSpellCheck( 'false' );
    

    函数参数可以是布尔值或字符串。

    无需循环遍历 textarea 内容,我们只需剪切并粘贴那里的内容,然后设置焦点。

    blink 引擎(Chrome(ium)、Edge 等)中测试

    【讨论】:

      【解决方案3】:

      我用这个问题来回答你的问题:Force spell check on a textarea in WebKit

      HTML:

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

      Javascript:

      $('#editor').focusin(function(){
          $(this).attr('spellcheck', true);
      });
      
      $('#editor').focusout(function() {
          $(this).attr('spellcheck', false);
          forceSpellcheck($(this));
      });
      
          var canCheck = true;
          function forceSpellcheck($textarea) {
        if (canCheck) {
          canCheck = false;
      
          $textarea.focus();
          $textarea.attr('spellcheck', false);
          var characterCount = $textarea.val().length;
      
          var selection = window.getSelection();
          for (var i = 0; i < characterCount; i++ ) {
            selection.modify("move", "backward", "character");
          }
      
          // Remove focus from the element, since the word under
          // the cursor won't have a misspelling marker.
          $textarea.blur();
        } else {
          canCheck = true;
        }
      }​
      

      演示:http://jsfiddle.net/QgsRU/13/

      【讨论】:

      • 常数为 1000 的循环看起来不太好。您可以使用.split(\s) 获取字数。解决这个问题,你就得到了我的支持。
      • 说实话我没有研究过那种方法。我只是复制粘贴。我会调查的。谢谢gdoron!
      • 它甚至根本不起作用。它挂了我的浏览器,我无法模糊它。
      • 真的吗?我没有问题。
      • 是的,你们是对的,我找到了问题所在。太多的模糊:P
      猜你喜欢
      • 2011-08-08
      • 2021-09-08
      • 1970-01-01
      • 2023-03-23
      • 2017-01-06
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多