只复制文本:
function copy() {
let body = document.querySelector('body');
let input = document.createElement('textarea');
input.value = '11111\n22222\n33333';
body.appendChild(input);
input.select();
document.execCommand('Copy');
alert('复制成功');
body.removeChild(input);
}
复制带超链接的信息:
安装clipboard
插件
npm i clipboard -S
import Clipboard from 'clipboard'
let body = document.querySelector('body');
let div = document.createElement('div');
let button = document.createElement('button');
div.style.backgroundColor = '#fff';
div.innerHTML = `<a href="www.baidu.com">百度一下</a>
<div>11111<br\>22222<br\>33333</div>`;
body.appendChild(div);
body.appendChild(button);
let myClipboard = new Clipboard(button, {
target: () => div
});
myClipboard.on('success', () => {
alert('复制成功');
body.removeChild(div);
body.removeChild(button);
});
button.click();