【问题标题】:Triggering spellcheck after attribute change属性更改后触发拼写检查
【发布时间】:2019-10-07 07:01:31
【问题描述】:

我有一个内容可编辑的 div,拼写检查设置为 false。我有一个按钮,单击该按钮会将拼写检查属性更改为 true,但在单击 div 内部之前,拼写检查不会启动。我曾尝试在 div 上触发点击、聚焦、模糊、更改等事件,但没有任何原因导致出现红线。这是在旧的 jQuery 1.8(旧版应用程序)中。这有什么诀窍吗?

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

我还将事件包装在 1 秒的 setTimeout 中以通过任何异步竞争条件,但没有运气。

HTML 部分:

<div id="editor" class="editor" contenteditable spellcheck="false"></div>
<button class="spellcheck">Check Spelling</button>
  • 这实际上不是 contenteditable 的问题,它也发生在普通 div 上。

【问题讨论】:

  • 能否也提供这个html部分?
  • 在原帖中添加了 HTML
  • tshimkus,阅读我的帖子。这是 jQuery 1.8。
  • 另外,这根本不是重复的。
  • 您可能希望考虑 cmets 的次要意外影响。如果您的目标是激怒@tshimkus,那么恭喜您,您成功了。如果您的目标是尝试找到解决方案并让其他人相信您值得他们努力帮助您,那么上面的 cmets 可能无法帮助您实现这一目标。

标签: javascript jquery contenteditable spell-checking


【解决方案1】:

单击按钮,它会添加spellcheck 属性,然后将焦点设置到#editor div。拼写检查处于活动状态并在拼写错误的单词下划线。

这里是 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>

这是不同 sn-p 中的相同代码,这个运行 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>

【讨论】:

  • 很奇怪,它在我们的应用程序中不起作用......完全相同的代码,我们在添加这个问题之前就尝试过。除非您手动单击它,否则不会触发红线。
  • 这有点奇怪......如果我在 contenteditable div 中输入“T‍est 2asas asasas asasas”(不带引号)并单击拼写检查按钮,它会起作用。但是,如果我输入“Thiis is teh wroong speling”,在我点击他的 div 之前它不起作用。我在 contenteditable div #editor 中设置了 lang="en"。
猜你喜欢
  • 2018-01-11
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2012-03-12
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多