美文网首页
js实现点击复制功能

js实现点击复制功能

作者: 上海_前端_求内推 | 来源:发表于2021-11-25 14:43 被阅读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>

相关文章

网友评论

      本文标题:js实现点击复制功能

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