2017-12-20 22 views
2

私たちのアプリケーションでは、HTML(テキストと書式)をクリップボードにコピーするために以下のロジックを使用しています。document.execCommand( "copy")はInternet Explorer 11で動作しなくなった

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>

それはすべての主要なブラウザ(クローム、Firefoxの、エッジおよびInternet Explorer)で正常に働いていました。

最新のInternet Explorerバージョン11.125.16299.0(Updateversion:11.0.49 - KB4052978)では、HTMLはクリップボードにコピーされなくなりました。

この下のセキュリティ設定があります:

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

が、私は「活性化」するために「依頼」から値を変更しました。これは効果がありません。

なぜ誰かが、何が変わったのか、おそらく別の解決策または回避策を知っていますか?ありがとうございました。

答えて

2

document.execCommand("copy")ではなく、document.execCommand('selectAll',false,null)です。 divの内容を視覚的に選択していますが(DOMから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("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>

関連する問題