js原生实现复制文本到剪切板

<span id="copy">{{yuming}}</span>
<el-button type="primary" plain size="mini" @click="copy()">复制</el-button>
copy(){
     var copyTest = document.getElementById("copy").innerText;
     var inputTest = document.createElement('input');
     inputTest.value = copyTest;
     document.body.appendChild(inputTest);
     inputTest.select();
     document.execCommand("Copy");
     inputTest.className = 'oInput';
     inputTest.style.display = 'none';
     alert('复制成功');
    }

 

posted @ 2020-10-26 20:55  冬冬先生  阅读(2371)  评论(0编辑  收藏  举报