美文网首页
js将内容复制至剪切板

js将内容复制至剪切板

作者: 青城墨阕 | 来源:发表于2020-09-17 16:51 被阅读0次
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>复制至剪切板DOM</title>
    <script>
        function myFunction(){
            var oInput = document.createElement('label');
            oInput.style = 'opacity: 0;position: absolute;left:-100px;top:-100px;';
            oInput.innerHTML = 'hello!!!';
            oInput.id = 'cpInput';
            document.body.appendChild(oInput);
            var range = document.createRange();
            range.selectNode(document.getElementById('cpInput'));
            var selection = window.getSelection();
            if (selection.rangeCount > 0) {
                selection.removeAllRanges();
            }
            selection.addRange(range);
            document.execCommand('copy');
            document.getElementById('cpInput').remove();
        }
    </script>
    </head>
    <body>
    
    <p>复制至剪切板DOM</p>
    <button onclick="myFunction()">点我复制</button>
    <p id="demo" style="display: inline;"></p>
    
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:js将内容复制至剪切板

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