2017-12-20 1 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>

그것은 모든 주요 브라우저 (크롬, 파이어 폭스, 에지 및 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>

관련 문제