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

JavaScript實(shí)現(xiàn)一鍵復(fù)制內(nèi)容剪貼板

 更新時(shí)間:2022年07月22日 14:45:51   作者:TANKING  
這篇文章主要為大家介紹了JavaScript實(shí)現(xiàn)一鍵復(fù)制內(nèi)容,document.execCommand原生JS設(shè)置剪貼板的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

引言

有時(shí)候?yàn)榱朔奖阌脩?hù)快速?gòu)?fù)制頁(yè)面的內(nèi)容,一般的做法就是添加一個(gè)按鈕給用戶(hù)點(diǎn)擊一下就復(fù)制下來(lái)了,這邊使用JavaScript原生的方法進(jìn)行設(shè)置剪貼板。

代碼

copy.html

<!DOCTYPE html>
<html>
<head>
    <title>一鍵復(fù)制demo</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover">
    <style type="text/css">
        *{
            padding:0;
            margin:0;
        }
        h2{
            text-align: center;
            margin-top: 20px;
        }
        #neirong{
            width: calc(90% - 20px);
            padding:10px 10px;
            margin:20px auto;
            background: #eee;
            border-radius: 5px;
        }
        #copy{
            border:none;
            width: 90%;
            height: 45px;
            background: #39f;
            font-size: 16px;
            color: #fff;
            font-weight: bold;
            border-radius: 5px;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>
<body>
<h2>一鍵復(fù)制demo</h2>
<div id="neirong">這是內(nèi)容這是內(nèi)容這是內(nèi)容這是內(nèi)容</div>
<button id="copy">復(fù)制</button>

<script>
function copyArticle(event){
      const range = document.createRange();
      range.selectNode(document.getElementById('neirong'));
      const selection = window.getSelection();
      if(selection.rangeCount > 0) selection.removeAllRanges();
      selection.addRange(range);
      document.execCommand('copy');
      alert("復(fù)制成功");
}

window.onload = function () {
  var obt = document.getElementById("copy");
  obt.addEventListener('click', copyArticle, false);
}
</script>
</body>
</html>

實(shí)現(xiàn)效果

以上就是JavaScript實(shí)現(xiàn)一鍵復(fù)制內(nèi)容剪切板的詳細(xì)內(nèi)容,更多關(guān)于JavaScript一鍵復(fù)制內(nèi)容的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論