在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js剪切板為什么沒有復(fù)制成功?

js剪切板為什么沒有復(fù)制成功?

有彈窗但是沒有復(fù)制成功
<script type="text/javascript">
window.onload=function(){

      function test(){
        var aux = document.createElement("input");
        var content = 'test';
        aux.setAttribute("value", content);
        document.body.appendChild(aux);
        aux.select();
        document.execCommand("copy");
        alert("復(fù)制成功");
      }
      test();

}
</script>

回答
編輯回答
清夢

clipboard.js 了解一下 這個更方便一點

2017年4月2日 06:38
編輯回答
背叛者

不是復(fù)制功能不行,是你放在onload里面不行,你放個button,點擊的時候調(diào)用test,就能運行了

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Test</title>
</head>
<body>
  <button onclick="test()">哈哈哈哈哈</button>
  <script> 
    function test() {
      var aux = document.createElement("input");
      var content = 'test';
      aux.setAttribute("value", content);
      document.body.appendChild(aux);
      aux.select();
      var res = document.execCommand("copy");
      if (res) {
        alert("復(fù)制成功");
      } else {
        alert("復(fù)制失敗");
      }
    }
  </script>
</body>
</html>

我覺得是瀏覽器處于安全的考慮,禁止了這種自動調(diào)用復(fù)制的功能,一定要用戶手動觸發(fā)下才可以

2017年3月23日 10:32
編輯回答
喵小咪

你這段代碼的彈窗必定彈出成功,跟是否復(fù)制成功沒有關(guān)系吧?
想正確提示得改成這樣

window.onload = function () {
  function test() {
    var aux = document.createElement("input");
    var content = 'test';
    aux.setAttribute("value", content);
    document.body.appendChild(aux);
    aux.select();
    var res = document.execCommand("copy");
    if (res) {
      alert("復(fù)制成功");
    } else {
      alert("復(fù)制失敗");
    }
  }
  test();
}

剛試了下 只有ie11下成功,chrome edge firefox 均失敗

2017年5月18日 04:21