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

H5 js點(diǎn)擊按鈕復(fù)制文本到粘貼板

 更新時(shí)間:2020年11月19日 12:01:49   作者:武當(dāng)山道士  
這篇文章主要為大家詳細(xì)介紹了H5 js點(diǎn)擊按鈕復(fù)制文本到粘貼板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了js點(diǎn)擊按鈕復(fù)制文本到粘貼板的具體代碼,供大家參考,具體內(nèi)容如下

準(zhǔn)備:先去下載clipboard.js: 官網(wǎng)
移動(dòng)端效果如下:

應(yīng)用:html+js 代碼如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" >
  <script src="/res/js/jquery.min.js"></script>
  <script type="text/javascript" src="/res/js/clipboard.min.js"></script>
  <style>

    body{
      font-weight: 300;
    }
    .code-box{
      border-radius: 5px;
      background: #f0830f;
      height: 80px;
      text-align: center;
    }
    .my-code{
      height: 20px;
      line-height: 20px;
      margin-top: 10px;
     }
    .btn-copy{
      height: 30px;
      line-height: 30px;
      border-radius: 15px;
      font-weight: 400;
      font-size: 14px;
      background-color: #fff1c8;
      color:#F06D4A;
      width: 40%;
      margin: 5px 30%;
    }
  </style>
</head>
<body>
    <div class="text-center">—— 復(fù)制邀請(qǐng)碼,好禮領(lǐng)回家 ——</div>
    <div class="code-box">
      <div class="my-code" id="code">{$code}</div>
      <div class="btn-copy" id="codeBtn" data-clipboard-target="#code">復(fù)制邀請(qǐng)碼</div>
    </div>

    <script>
     $(".btn-copy").click(function(){
     //實(shí)例化clipboard
     var clipboard = new ClipboardJS('#codeBtn');
     clipboard.on("success", function(e){
       alert('復(fù)制成功');
       e.clearSelection();
       clipboard.destroy(); // 如果不銷毀,第一次以后的復(fù)制,會(huì)有重復(fù)的alert彈出
     });
     clipboard.on("error", function(e){
       alert("復(fù)制失敗,請(qǐng)手動(dòng)復(fù)制!");
       clipboard.destroy(); // 如果不銷毀,第一次以后的復(fù)制,會(huì)有重復(fù)的alert
     });
   });
  </script>
</body>
</html>

注意:

一定要銷毀對(duì)象 clipboard.destroy();
如果不銷毀,第一次以后的復(fù)制,會(huì)有重復(fù)的alert

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論