应用场景:直接对系统剪切板赋值仅支持IE浏览器,如谷歌等其他浏览器无法使用。
<script>
function twqh() {
let transfer = document.createElement('input');//创建控件
document.body.appendChild(transfer);
transfer.style.cssText = 'position: absolute;right: 45%;top: 80%;'
transfer.value = document.getElementById('div1').innerText; // 这里表示想要复制的内容
transfer.focus();
transfer.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
}
transfer.blur();
console.log('复制成功');
document.body.removeChild(transfer);//删除控件
}
</script>
使用onclick事件即可