JavaScript实现一键复制内容剪贴板

<!DOCTYPE html>
<html>
  <head>
    <title>一键复制demo</title>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width,initial-scale=1,maximum-scale=1,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: 10px10px;
        margin: 20pxauto;
        background: #eee;
        border-radius: 5px;
      }
      #copy {
        border: none;
        width: 90%;
        height: 45px;
        background: #39f;
        font-size: 16px;
        color: #fff;
        font-weight: 700;
        border-radius: 5px;
        margin: 0auto;
        display: block;
      }
    </style>
  </head>
  <body>
    <h2>一键复制demo</h2>
    <div id="neirong">这是内容这是内容这是内容这是内容</div>
    <button id="copy">复制</button>
    <script>
      functioncopyArticle(event){

      constrange=document.createRange();

      range.selectNode(document.getElementById('neirong'));

      constselection=window.getSelection();

      if(selection.rangeCount>0)selection.removeAllRanges();

      selection.addRange(range);

      document.execCommand('copy');

      alert("复制成功");

      }

      window.onload=function(){

      varobt=document.getElementById("copy");

      obt.addEventListener('click',copyArticle,false);

      }
    </script>
  </body>
</html>

给TA打赏
共{{data.count}}人
人已打赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索