Javascript 複製文字到剪貼簿(input hidden)

如何點擊按鈕,複製一段文字功能。

1
2
3
4
5
6
7
8
9
10
11
12
<input type="hidden" id="copy_url" value="https://www.google.com">
<div class="copy_div">複製連結</div>

<script>
$(".copy_url").on('click', function () {
var copyText = document.getElementById("copy_url");
copyText.type = 'text';
copyText.select();
document.execCommand("Copy");
copyText.type = 'hidden';
});
</script>

參考文章: https://stackoverflow.com/questions/31593297/using-execcommand-javascript-to-copy-hidden-text-to-clipboard