javascript - 无法使用 execCommand 在 IE11 div contentEditable 中删除或插入文本

标签 javascript internet-explorer-11

document.execCommand("delete");
document.execCommand("insertText", false, insertString);

以上两个命令在 Chrome/Firefox 中工作得很好,但我无法在 IE11 中使用它。

document.execCommand("delete") 仅当我们选择一段文本时才能在 IE 中使用。我如何让它像在 Chrome 中一样工作(即像使用退格键一样)。

还有我如何在 IE 中使用 document.execCommand("insertText", false, insertString)

最佳答案

对于 IE 中的 insertText,有解决方法:

 let isIE = function() {
    return document.all || (!!window.MSInputMethodContext && !!document.documentMode);
 }

 let text = "Hello"
 if(isIE()) document.execCommand("paste", false, text);

关于javascript - 无法使用 execCommand 在 IE11 div contentEditable 中删除或插入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247049/

相关文章:

javascript - 将范围标记的值设置为 :before content

Javascript-如何通过Chartist js在饼图上制作覆盖标签

javascript - Laravel,jquery - 根据选择框填充输入框

angular - 在 Internet Explorer 11 中使用 Angular Elements 的推荐方法

selenium - 使用 SendKeys 向 IE 11 发送 '@' 特殊字符

javascript - 在 AngularJS 中返回范围变量的长度

javascript - 未捕获的类型错误 : Failed to construct 'MouseEvent' : Failed to read the 'view' property from 'UIEventInit' : Failed to convert value to 'Window'

css - 为什么我的 CSS 网格不能在 IE 11 中正确呈现?

IE11 上的 Angular 4 性能/CPU 使用率

html - 4 列 Flexbox-Layout 在任何浏览器上都能很好地工作,除了 IE,它在一行中只显示 3 列