2017-10-02 1 views
1

<div>어떻게 마크 업을 복사 - 그냥 일반 텍스트가 아닌를 - 클립 보드에 자바 스크립트

<div id="myElement"> 
    Mary had a little <b>lamb</b>, its <i>fleece</i> was white as snow. Everywhere that mary went, the lamb was sure to go. 
</div> 

의 전체 콘텐츠를 무료로 유산을 사용하여이 코드

const copyAll = document.querySelector("myElement"); 
window.getSelection().selectAllChildren(copyAll); 
document.execCommand("Copy"); 
window.getSelection().removeAllRanges(); 

를 사용하여 클립 보드에 복사해야 (execCommand가 작동하려면 이벤트 리스너를 통해 호출을 실행해야합니다.)

텍스트 내용 만입니다. 복사 됨. 기존의 firefox 클립 보드 애드온에 의존하는 및 <b> 태그) 없이 복사 할 수 있습니까?

답변

2

필자는 사용자 정의 텍스트를 클립 보드에 복사하는 가짜 텍스트 영역 요소를 사용하여이 문제를 해결했습니다. 이 같은

뭔가 도움이 될 것입니다

const copyText = document.querySelector("myElement").innerHTML; 
copyToClipboard(copyText); 


function copyToClipboard(message) { 
     let textArea = document.createElement("textarea"); 
     textArea.value = message; 
     document.body.appendChild(textArea); 
     textArea.select(); 
     document.execCommand("copy"); 
     document.body.removeChild(textArea); 
    } 
+0

니스 우회 - 다른 옵션이없는 경우,이 가능한 솔루션이 될 것입니다. 고맙습니다. –