js实现点击复制功能

2021-11-25  本文已影响0人  上海_前端_求内推
<!DOCTYPE html>
<html>
<head>
    
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport" id="viewport">
    <style type="text/css">
    </style>
</head>
    <body>
        <div class="">
                <span id="copy_p" style="font-weight:bold;">测试1</span>
                <p id="copy">点击复制微信号</p>
        </div>  
    </body>
</html>
<script type="text/javascript">
     function copyArticle(event){
        const range = document.createRange();
        range.selectNode(document.getElementById('copy_p'));

        const selection = window.getSelection();
        if(selection.rangeCount > 0) selection.removeAllRanges();
        selection.addRange(range);

        document.execCommand('copy');
        alert('复制成功');
    }       
    document.getElementById('copy').addEventListener('click', copyArticle, false);
</script>

上一篇 下一篇

猜你喜欢

热点阅读