前端实现复制有两种方法:
- 使用document.execCommand('Copy);
- 使用Selection对象
const handleCopyToken = (token) => {
const textArea = document.createElement('textArea');
textArea.value = token;
document.body.appendChild(textArea);
textArea.select();
try {
if (document.execCommand('Copy')) {
Message.success(`${t('dataset.copySuccess')}`);
} else {
Message.error(`${t('dataset.copyFailed')}`);
}
} catch (err) {
Message.error(`${t('dataset.copyFailed')}`);
}
document.body.removeChild(textArea);
};
const copyCellData = (row, column, cell) => {
const item = cell.getElementsByClassName('cell')[0];
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(item);
selection.removeAllRanges();
selection.addRange(range);
};