텍스트 복사 / 붙여넣기 자바스크립트

2018. 3. 9. 16:01Javascript/Javascript+jQuery

반응형
<html>
<head>
<script>

function pasteboard(){

textRange = document.body.createTextRange();
textRange.moveToElementText(test);
textRange.execCommand("paste");
}

function clipboard(){
holdtext.createTextRange().execCommand("Copy");
}

</script>
</head>

<body>

<textarea id="holdtext"></textarea>

<button onclick="clipboard();">복사</button>
<button onclick="pasteboard();">가져오기</button>

<textarea id="test"></textarea>

</body>
</html>




<div id="divid">Hello This div content have to be select.</div>

<button onclick="selectText('divid');">Select Div</button>


function selectText(element) {

    var doc = document

        , text = doc.getElementById(element)

        , range, selection

    ;    

    if (doc.body.createTextRange) { //ms

        range = doc.body.createTextRange();

        range.moveToElementText(text);

        range.select();

    } else if (window.getSelection) { //all others

        selection = window.getSelection();        

        range = doc.createRange();

        range.selectNodeContents(text);

        selection.removeAllRanges();

        selection.addRange(range);

    }

}



<textarea id="input">Some text to copy.</textarea>
<button id="copy-button">Copy</button>

<script>
    var input  = document.getElementById("input");
    var button = document.getElementById("copy-button");

    button.addEventListener("click", function (event) {
        event.preventDefault();
        input.select();
        document.execCommand("copy");
    });
</script>


반응형