zoukankan      html  css  js  c++  java
  • JavaScript实现复制文本功能,兼容ie

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
      </head>
      <body>
        <button onclick="copyText()">copy</button>
        <input type="text" />
        <p id="text">很多文本,很多文本123</p>
        <script>
          function copyText() {
            var copyText = document.getElementById('text').innerText;
            // ie浏览器
            if (window.clipboardData) {
              window.clipboardData.clearData();
              window.clipboardData.setData("Text", copyText);
              alert("复制成功!");
            } else {//非ie
              var oInput = document.createElement("input");
              oInput.value = copyText;
              document.body.appendChild(oInput);
              oInput.select();
              document.execCommand("Copy");
              oInput.style.display = "none";//隐藏放最后,要不不生效
            }
          }
        </script>
      </body>
    </html>
    
  • 相关阅读:
    2016-02-24 工作日记
    金字塔培训
    你找到自己的路了么?
    你是个成熟的职场人么?
    码农十年总结
    码农十年连载六
    码农十年连载五
    码农十年连载四
    码农十年连载三
    码农十年连载二
  • 原文地址:https://www.cnblogs.com/samsara-yx/p/14612000.html
Copyright © 2011-2022 走看看