美文网首页
execCommand被废弃,复制文本该使用什么方法

execCommand被废弃,复制文本该使用什么方法

作者: 4ANH | 来源:发表于2022-02-17 14:38 被阅读0次

    使用navigator.clipboard.writeText(value);相比以前更加简洁,采用promise实现

    //旧:
    let oInput = document.createElement('input');
    oInput.value = value;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象;
    document.execCommand("Copy"); // 执行浏览器复制命令
    this.$Message.success('复制成功');
    oInput.remove()
    
    //新:
    navigator.clipboard.writeText(value).then(() => {
      this.$Message.success('复制成功');
    });
    

    相关文章

      网友评论

          本文标题:execCommand被废弃,复制文本该使用什么方法

          本文链接:https://www.haomeiwen.com/subject/vkorlrtx.html