js复制文本

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <p class="DzWx">
        <span class="wx">微信:<span id="wxContent"> abcdefg123456</span></span>
        <button class="copy" id="copyId">复制</button>
     </p>
</body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
    $(".copy")[0].onclick = function(){
        var txt = $('#wxContent').text();
        Copy(txt);
    }
    // 复制微信号函数
    function Copy(str) {
        var save = function(e) {
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        };
        document.addEventListener('copy', save);
        document.execCommand('copy');
        document.removeEventListener('copy', save);
        console.log('复制成功');
    }
</script>
</html>
原文地址:https://www.cnblogs.com/wuqilang/p/12167109.html