댓글 검색 목록

[javascript] 클립 보드에 텍스트 복사 (14/86)

페이지 정보

작성자 운영자 작성일 20-03-31 16:22 조회 842 댓글 0

주어진 텍스트, 텍스트를 클립 보드에 복사한다고 가정합니다.


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);
}






댓글목록 0

등록된 댓글이 없습니다.

웹학교 로고

온라인 코딩학교

코리아뉴스 2001 - , All right reserved.