简体   繁体   English

为什么document.execCommand(“ copy”)在Internet Explorer 11中不再起作用?

[英]Why is document.execCommand(“copy”) no longer working in Internet Explorer 11?

In our application we are using the following logic to copy HTML (text and format) to the clipboard. 在我们的应用程序中,我们使用以下逻辑将HTML(文本和格式)复制到剪贴板。

 function copy(element_id) { var aux = document.createElement("div"); aux.setAttribute("contentEditable", true); aux.innerHTML = document.getElementById(element_id).innerHTML; aux.setAttribute("onfocus", "document.execCommand('selectAll',false,null)"); document.body.appendChild(aux); aux.focus(); document.execCommand("copy"); document.body.removeChild(aux); console.log("COPY"); } 
 <p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p> <button onclick="copy('demo')">Copy To Clipboard Keeping Format</button> 

It was working fine in all major Browsers (Chrome, Firefox, Edge and Internet Explorer). 在所有主要的浏览器(Chrome,Firefox,Edge和Internet Explorer)中,它都运行良好。

With the latest Internet Explorer version 11.125.16299.0 (Updateversion: 11.0.49 - KB4052978) the HTML is no longer copied to the clipboard. 使用最新的Internet Explorer版本11.125.16299.0(更新版本:11.0.49-KB4052978),HTML不再复制到剪贴板。

There is a security setting for this under: 在下面有一个安全设置:

Options -> Security -> Edit level ... -> Scripting -> Allow access to clipboard

I changed the value from "Ask" to "Activated". 我将值从“询问”更改为“已激活”。 This has no effect. 这没有作用。

Does anybody know why, what they changed and maybe another solution or workaround? 有谁知道为什么,他们做了什么更改,也许还有其他解决方案或解决方法? Thank you. 谢谢。

It turns out that the problem was not document.execCommand("copy") , but document.execCommand('selectAll',false,null) . 事实证明问题出在不是document.execCommand("copy") ,而是document.execCommand('selectAll',false,null) While it does visually select the content of the div (you can see it, if you don't remove it from the DOM) the selection is not recognized by the copy command. 尽管它确实可以直观地选择div的内容(如果不从DOM中删除它,则可以看到它),但是复制命令无法识别该选择。

The following code works: 以下代码有效:

 function copy(element_id) { var aux = document.createElement("div"); aux.setAttribute("contentEditable", true); aux.innerHTML = document.getElementById(element_id).innerHTML; document.body.appendChild(aux); window.getSelection().selectAllChildren(aux); document.execCommand("copy"); document.body.removeChild(aux); console.log("COPY"); } 
 <p id="demo"><b>Bold text</b> and <u>underlined text</u>.</p> <button onclick="copy('demo')">Copy To Clipboard Keeping Format</button> 

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

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