Hidden element won't copy to clipboard

后端 未结 4 1914
闹比i
闹比i 2021-01-13 19:20

I am trying to add button to copy simple text string but without success.

4条回答
  •  庸人自扰
    2021-01-13 19:52

    You can't .select() a hidden element that has visibility: hidden; or display: none; but you can do something like this:

    function kopiraj() {
      var copyText = document.getElementById("toCopy");
      copyText.select();
      document.execCommand("Copy");
    }
    [aria-hidden="true"] {
      opacity: 0;
      position: absolute;
      z-index: -9999;
      pointer-events: none;
    }
    
    

提交回复
热议问题