execCommand("copy")

 

execCommand("copy") JavaScript 指令就是執行複製功能

<!DOCTYPE html>
<html>

<head>
    <style>
    </style>
</head>

<body>
    <input id="ori" type="text" value="abc">
    <input id="paste" type="text" value="fff">
    <script>
        document.getElementById("ori").addEventListener("click", function () {
            this.select();
            document.execCommand("copy");
        });

        document.getElementById("paste").addEventListener("focus", function () {
            this.select();
            document.execCommand("paste");
        });
    </script>
</body>

</html>

需特別注意的是,document.execCommand("paste"); 只在 IE 有作用而已,

於 Edge、Chrome 皆未實作 document.execCommand("paste");

 

參考資料:

clipboard.js

HTML DOM execCommand() Method

garykac/clipboard