js:
copy = () => {
try {
let range = document.createRange();
// this.linkRef.current是react中的ref
range.selectNode(this.linkRef.current);
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
// Toast是Antd的
Toast.success('复制成功', 1);
} catch (e) {
Toast.fail('复制失败', 1);
}
};
html:
<span onClick={this.copy} className="text-center d-block text-write">复制链接</span>