方法一:通过获取id选择器

// html内容
<div id='loginAddress'>我要复制的内容</div> <el-button type="info" @click="copy">复制链接</el-button>
// js内容 // 复制链接 copy() { var Url = document.getElementById("loginAddress"); Url.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令 },

 

方法二:先拿到值,在进行复制操作

const copyContent = "我要复制的内容";
                    // 创建input标签存放需要复制的文字
                    const inputTag = document.createElement("input");
                    // 把文字放进input中,供复制
                    inputTag.value = copyContent;
                    document.body.appendChild(inputTag);
                    // 选中创建的input
                   inputTag.select();
                    // 执行复制方法, 该方法返回bool类型的结果,告诉我们是否复制成功
                    const copyResult = document.execCommand("copy");
                    // 操作中完成后 从Dom中删除创建的input
                    document.body.removeChild(inputTag);
                    // 根据返回的复制结果 给用户不同的提示
                    if (copyResult) {
                      this.successTip("复制成功!"); // 自己定义的提示语
                    } else {
                      this.warningTip("复制失败!"); // 自己定义的提示语
                    }
posted on 2020-05-07 17:33  華落星星的眼眸  阅读(331)  评论(0编辑  收藏  举报