Hidden element won't copy to clipboard

后端 未结 4 1929
闹比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 20:00

    I've successfully used this JavaScript code which doesn't require any new HTML elements:

    var text = ...;
    var listener = function(ev) {
        ev.clipboardData.setData("text/plain", text);
        ev.preventDefault();
    };
    document.addEventListener("copy", listener);
    document.execCommand("copy");
    document.removeEventListener("copy", listener);
    

提交回复
热议问题