简体   繁体   English

属性更改后触发拼写检查

[英]Triggering spellcheck after attribute change

I have a contenteditable div with spellcheck set to false. 我有一个contenteditable div,且其checkcheck设置为false。 I have a button that when clicked, changes the spellcheck attribute to true, but the spellcheck won't kick in until I click inside the div. 我有一个按钮,当单击该按钮时,会将spellcheck属性更改为true,但是直到我在div内单击后,拼写检查才会生效。 I have tried triggering events for click, focus, blur, change etc. on the div, and nothing causes the red lines to appear. 我尝试在div上触发单击,焦点,模糊,更改等事件,但没有任何原因导致出现红线。 This is in old jQuery 1.8 (legacy app). 这是在旧的jQuery 1.8(旧版应用程序)中。 is there a trick to this? 这有招吗?

$('.spellcheck').live('click', function() {
    $('#editor').attr('spellcheck', true);
    $('#editor').click();
    $('#editor').focus();
    $('#editor').blur();
    $('#editor').change();
});

I have also wrapped the events in a 1 second setTimeout to get past any asynchronous race conditions, but no luck. 我也将事件包装在1秒的setTimeout中,以克服任何异步竞争条件,但没有运气。

HTML Part: HTML部分:

<div id="editor" class="editor" contenteditable spellcheck="false"></div>
<button class="spellcheck">Check Spelling</button>
  • this isn't really a problem of contenteditable, it happens on a normal div as well. 这实际上不是contenteditable的问题,它也发生在普通div上。

Click the button, it adds the spellcheck attribute and then sets focus to the #editor div. 单击按钮,它将添加spellcheck属性,然后将焦点设置到#editor div。 Spellcheck is active and underlining misspelled words. 拼写检查处于活动状态,并在拼写错误的单词下划线。

Here it is in jQuery 1.8.1 : jQuery 1.8.1中

 $('.spellcheck').click( function () { $('#editor').attr('spellcheck', true); $('#editor').focus(); }); 
 #editor { width: 200px; height: 100px; border: 1px solid #e0e0e0; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <div id="editor" contenteditable spellcheck="false"> somme mispellled wordds </div> <button class="spellcheck">Check Spelling</button> 

Here's the same code in a different snippet, this one running jQuery 1.2.3 : 这是不同代码段中的相同代码,该代码运行jQuery 1.2.3

 $('.spellcheck').click( function () { $('#editor').attr('spellcheck', true); $('#editor').focus(); }); 
 #editor { width: 200px; height: 100px; border: 1px solid #e0e0e0; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <div id="editor" contenteditable spellcheck="false"> somme mispellled wordds </div> <button class="spellcheck">Check Spelling</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM