温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

兼容多浏览器的原生js复制函数copyText怎么使用

发布时间:2023-05-04 17:44:24 来源:亿速云 阅读:109 作者:iii 栏目:开发技术

今天小编给大家分享一下兼容多浏览器的原生js复制函数copyText怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

JS复制文本到剪切板 copyText

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>
    <body>
        <span class="orange" id="shareUrl" data-url="www.baidu.com">www.baidu.com</span>
        <script>
            // 复制
            function copyText(text) {
                var textArea = document.createElement("textarea");
                // textArea.style['display']='none'
                textArea.style['position'] = 'absolute'
                textArea.style['top'] = '0'
                textArea.style['left'] = '0'
                textArea.value = text;
                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();
                try {
                    var successful = document.execCommand('copy');
                    var msg = successful ? 'successful' : 'unsuccessful';
                    console.log(msg)
                } catch (err) {
                    console.error('复制失败', err);
                }
                document.body.removeChild(textArea);
                if (successful) {
                    return true
                }
            };
            $(function() {
                var shareUrl = $("#shareUrl").data("url");
                $("#shareUrl").click(function() {
                    var shareUrl = $("#shareUrl").data("url");
                    if (copyText(shareUrl)) {
                        alert("复制成功");
                    }
                })
            })
        </script>
    </body>
</html>

删减后的代码,减少了判断,其实上面的代码是非常好的

function copyText (text) {
        //生成一个textarea对象
      var textArea = document.createElement('textarea');
        //设置属性
      textArea.style.position = 'fixed';
      textArea.style.top = 0;
      textArea.style.left = 0;
      textArea.style.width = '2em';
      textArea.style.height = '2em';
      textArea.style.padding = 0;
      textArea.style.border = 'none';
      textArea.style.outline = 'none';
      textArea.style.boxShadow = 'none';
      textArea.style.background = 'transparent';
      textArea.value = text;
      //添加到页面body
      document.body.appendChild(textArea);
      textArea.select();
      //执行
        var msg = document.execCommand('copy') ? '成功' : '失败';
        Popup('复制内容' + msg);
       //移除对象
      document.body.removeChild(textArea);
    } 
function Popup(message){
            var span=document.createElement('span')
            span.innerHTML=message || 'default'
            span.className='popupStyle'
            span.style.display='block'
            document.body.appendChild(span)
            setTimeout(()=>{
                span.remove()
            },1000)
        }

对了不要忘了css样式

.popupStyle{
            width:180px;
            height:50px;
            background-color: rgb(85,85,85);            
            /* display:none; */
            color:#fff;
            text-align:center;
            line-height:50px;
            border-radius:5px;
            padding:0;
            position:fixed;
            z-index:1;
            top:30%;
            left:50%;
            transform:translateX(-50%);
        }

以上就是“兼容多浏览器的原生js复制函数copyText怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI