简体   繁体   English

复制内容内的点击 <div> 使用拼写检查

[英]Replicate click inside contenteditable <div> with spellcheck

Chrome's native spell check won't work on a contenteditable <div> unless the user clicks into the <div> , which makes sense. 除非用户单击<div> ,否则Chrome的本机拼写检查无法在可编辑内容的<div>上使用。 But if I'm adding a contenteditable <div> dynamically, is there a way to replicate a user clicking into the <div> > so that the spell check will work? 但是,如果我要动态添加一个内容可编辑的<div> ,是否可以复制用户单击<div>的用户,以便进行拼写检查? I tried using jQuery: 我尝试使用jQuery:

 $('#div-id').click();

and

 $('#div-id').trigger('click');

but these didn't work. 但是这些没有用。 Any help? 有什么帮助吗? jQuery or JavaScript works for me. jQuery或JavaScript对我有效。

As a comment mentioned, bringing focus to the element programmatically will work to enable spellcheck. 如前所述,以编程方式将重点放在元素上将有助于启用拼写检查。 But this might be undesirable due to the focus now being changed to another element. 但这可能是不希望的,因为现在焦点已更改为另一个元素。 So here is a complete solution (not using jQuery but will still work with it): 因此,这是一个完整的解决方案(不使用jQuery,但仍可以使用它):

var oldFocus = document.activeElement;

div.focus();

if (oldFocus) {
    oldFocus.focus();
} else {
    div.blur();
}

This saves the previously focused element before focusing the div in order to enable spellcheck. 这样可以在聚焦div之前保存先前聚焦的元素,以启用拼写检查。 Then, the focus is returned to the old element (if there was one that already had focus), otherwise it makes nothing focused. 然后,将焦点返回到旧元素(如果有一个已经具有焦点),否则它不会使任何焦点。

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

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