1.使用原生方法 document.execCommand('copy')
<input id="clipInput" type="text" value="">
<button onclick="copy()">点击复制</button>
<script>
function copy(){
const input = document.querySelector('#clipInput');
input.value = "hello world";
input.select(); // 将input选中
if (document.execCommand('copy')) {
document.execCommand('copy'); // 将内容复制到剪贴板,相当于 Ctrl+C
console.log('复制成功');
}
}
</script>
点击复制操作时,借用input标签,将想要保存的内容先存储在value中,通过.select()操作选中保存内容的input
再使用 document.execCommand('copy') 复制到剪贴板 就完成啦~!
document.execCommand('copy') 就相当于 Ctrl+ C操作!!!
Ctrl + V 就能看到自己复制到剪贴板里的内容啦!
我的使用场景: 与APP互通某个参数!