주어진 텍스트, 텍스트를 클립 보드에 복사한다고 가정합니다.
https://htmldom.dev/copy-text-to-the-clipboard
이를 위해 텍스트 값을 가진 가짜 textarea 요소를 만듭니다. 다음으로 내용을 선택하고 "복사"명령을 실행합니다.
// Create a fake textarea
const textAreaEle = document.createElement('textarea');
// Reset styles
textAreaEle.style.border = '0';
textAreaEle.style.padding = '0';
textAreaEle.style.margin = '0';
// Set the absolute position
// User won't see the element
textAreaEle.style.position = 'absolute';
textAreaEle.style.left = '-9999px';
textAreaEle.style.top = `0px`;
// Set the value
textAreaEle.value = text;
// Append the textarea to body
document.body.appendChild(textAreaEle);
// Focus and select the text
textAreaEle.focus();
textAreaEle.select();
// Execute the "copy" command
try {
document.execCommand('copy');
} catch (err) {
// Unable to copy
} finally {
// Remove the textarea
document.body.removeChild(textAreaEle);
}
등록된 댓글이 없습니다.