document.execCommand(“复制”)不再在Internet Explorer 11中运行 [英] document.execCommand("copy") no longer working in Internet Explorer 11

查看:184
本文介绍了document.execCommand(“复制”)不再在Internet Explorer 11中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们使用以下逻辑将HTML(文本和格式)复制到剪贴板。 jsdata-hide =falsedata-console =truedata-babel =false>

 函数副本(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( 拷贝); document.body.removeChild(AUX); console.log(COPY);}  

< p id =demo>< b>粗体文字< / b>和&u;下划线文字< / u>< / p> < / /按钮>< / code>< / pre>

在所有主流浏览器(Chrome,Firefox,Edge和Internet Explorer)中都能正常工作。


$ b 使用最新的Internet Explorer版本11.125.16299.0(更新版本:11.0.49 - KB4052978)HTML不再复制到剪贴板。



有一个安全设置:

 选项 - >安全 - >编辑级别...  - >脚本 - >允许访问剪贴板

我将值从Ask更改为Activated。这没有任何作用。



有人知道他们为什么,他们改变了什么,或许是另一种解决方案或解决方法?谢谢。

解决方案

事实证明问题并非 document.execCommand(copy) ,但 document.execCommand('selectAll',false,null)。虽然它可以直观地选择 div 的内容(如果您不从DOM中移除它,您可以看到它),但复制命令无法识别该选择。



以下代码有效:

  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( 拷贝); document.body.removeChild(AUX); console.log(COPY);}  

< p id =demo>< b>粗体文字< / b>和&u;下划线文字< / u>< / p> < / /按钮>< / code>< / pre>

In our application we are using the following logic to copy HTML (text and format) to the clipboard.

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).

With the latest Internet Explorer version 11.125.16299.0 (Updateversion: 11.0.49 - KB4052978) the HTML is no longer copied to the clipboard.

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). 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.

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>

这篇关于document.execCommand(“复制”)不再在Internet Explorer 11中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆