欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

至2023年最好用的兼容多瀏覽器的原生js復(fù)制函數(shù)copyText

 更新時間:2023年05月02日 16:39:18   作者:前端白雪  
因為后臺需要增加一些復(fù)制一些內(nèi)容非表單中內(nèi)容,那么下面這個函數(shù)就非常的好用了,其實也是利用了表單的數(shù)據(jù)權(quán)限比較容易突破,下面是具體的實現(xiàn)函數(shù),大家可以拿走

JS復(fù)制文本到剪切板 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>
            // 復(fù)制
            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('復(fù)制失敗', 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("復(fù)制成功");
                    }
                })
            })
        </script>
    </body>
</html>

腳本之家小編刪減后的代碼,減少了判斷,其實上面的代碼是非常好的

function copyText (text) {
        //生成一個textarea對象
      var textArea = document.createElement('textarea');
        //設(shè)置屬性
      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();
      //執(zhí)行
        var msg = document.execCommand('copy') ? '成功' : '失敗';
        Popup('復(fù)制內(nèi)容' + 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%);
        } 

對于之前的一些代碼,可以當(dāng)個學(xué)習(xí)參考,原生js實現(xiàn)。

您可能感興趣的文章:

相關(guān)文章

最新評論