2017-12-20 1 views
2

In unserer Anwendung verwenden wir die folgende Logik, um HTML (Text und Format) in die Zwischenablage zu kopieren.document.execommand ("copy") funktioniert nicht mehr in 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>

Es funktioniert in allen gängigen Browsern (Chrome, Firefox, Kanten- und Internet Explorer) in Ordnung.

Mit dem neuesten Internet Explorer Version 11.125.16299.0 (Updateversion: 11.0.49 - KB4052978) wird der HTML-Code nicht mehr in die Zwischenablage kopiert.

Es ist eine Sicherheitseinstellung für diese unter:

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

ich den Wert von „Ask“ auf „Aktiviert“. Dies hat keine Auswirkung.

Weiß jemand, warum, was sie geändert haben und vielleicht eine andere Lösung oder Workaround? Vielen Dank.

Antwort

2

Es stellt sich heraus, dass das Problem nicht document.execCommand("copy"), sondern document.execCommand('selectAll',false,null) war. Während es visuell den Inhalt des div auswählt (Sie können es sehen, wenn Sie es nicht aus dem DOM entfernen) wird die Auswahl nicht durch den Kopierbefehl erkannt.

Der folgende Code funktioniert:

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>

Verwandte Themen