JS复制内容到剪贴板(vue)

MDN地址,里面有参数和命令:https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand

链接:<input size="mini" :value="item.downloadLink" :id="item.id"/>
     <el-button size="mini" type="primary" @click="copyUrl(item)">复制链接</el-button>



methods:{
   // 复制网址
    copyUrl(ele){
        //ele.id为我定义的变量id,获取input节点
        let inputText = document.getElementById(ele.id);
        let currentFocus = document.activeElement;
        inputText.focus();
        inputText.setSelectionRange(0, inputText.value.length);
        //复制步骤,copy为浏览器命令
        if(document.execCommand('copy', true)){
            this.$message({
                type:'success',
                message:'已成功复制到剪贴板'
            })
        }
        currentFocus.focus();
    },
}

猜你喜欢

转载自blog.csdn.net/xr510002594/article/details/85468324