会没有

您现在的位置是:首页 > 前端 > 正文

前端

JS, javascript,copy ,execCommand, js 复制 文本。

dracarys2023-02-28前端295
利用document的命令,用按钮复制文本。注意:execCommand即将废弃。以下是参考代码:    const range =&nbs

利用document的命令,用按钮复制文本。
注意:execCommand即将废弃。

以下是参考代码:

    const range = document.createRange();
    range.selectNode(document.getElementById('you div id')!);
    const selection = window.getSelection();
    if (selection!.rangeCount > 0) {
      selection?.removeAllRanges();
    }
    selection?.addRange(range);
    document.execCommand('copy');
    Toast.show('复制成功', 1);
    if (selection!.rangeCount > 0) {
      selection?.removeAllRanges();
    }