본문 바로가기

jQuery

jquery - 클립보드 복사하기

아래 소스 실행시켜보세요.



<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function(){

 

});

  function copyClipboard() {

  var $temp = $("<input>");

  $("body").append($temp);

  $temp.val($("#copycontent").val()).select();

  document.execCommand("copy");

  $temp.remove();

}


function copyClipboard2(selector) {

  var $temp = $("<input>");

  $("body").append($temp);

  $temp.val($(selector).text()).select();

  document.execCommand("copy");

  $temp.remove();

}


</script>

</head>

<body>


<button onclick="copyClipboard()">복사하기</button>

<button onclick="copyClipboard2('#p2')">복사하기2</button>

<p id="p2">Click me too!</p>

<input type="hidden" id="copycontent" value="안녕하세요">

<br/><br/>

<input type="text" placeholder="Paste here for test" />

</body>

</html>

반응형

'jQuery' 카테고리의 다른 글