zoukankan      html  css  js  c++  java
  • 使用JS实现复制粘贴功能

    使用JS实现复制粘贴功能

    如果嵌套太多使用这个:

    
    
    // 複製單號1
    // 第一步把這個放到頁面
    // <div style="position:absolute; opacity: 0;" id="copy1"></div>
    // 第二步增加按鈕設置點擊事件
    // 第三如下:
    copyNo() {
    const a = document.createElement('textarea')
    let b;
    if (this.myshow) {
    a.innerText = this.aaa[this.aaa.ccc] || '';
    } else {
    a.innerText = this.bb|| '';
    }
    b = document.getElementById('copy1')
    a.setAttribute('id', 'copy');
    b.innerHTML = "";
    b.appendChild(a)
    let e: any = document.getElementById("copy");
    e.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    if (a.value) {
    alert('Copy Success!');
    } else {
    alert('The copied content is empty!');
    }
    }
    // 複製單號2
     

    如果是简单地输入框没有太多嵌套,使用这个:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function copyLink(){
        var e = document.getElementById("copy");
        e.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令
        alert("内容复制成功!");
    }
    </script>
    </head>
    <body>
    <!-- <textarea id="copy">待复制的内容</textarea> -->
    <input type="text"  id="copy">
    <input type="button" onclick="copyLink()" value="点击复制"></input>
    </body>
    </html>
  • 相关阅读:
    cf 1155 d 最大区间和(变形 区间*x)
    俄罗斯方块的形状暴力
    cf 1160 E dp 组合数 思维
    cf 1110d dp(题目特殊性质)
    cf 1114d 区间dp 0,1标记左右
    poj 1426 bfs
    poj 1679 最小生成树是否唯一
    cf 1106e dp
    【PAT顶级】1002 Business (35分)(0/1背包,DP)
    【PAT顶级】1001 Battle Over Cities
  • 原文地址:https://www.cnblogs.com/sugartang/p/12066799.html
Copyright © 2011-2022 走看看