Copy string to clipboard, without using a DOM element?

折月煮酒 提交于 2019-12-10 14:39:56

问题


I have this code that works:

let textarea = document.createElement('textarea');
textarea.setAttribute('type', 'hidden');
textarea.textContent = 'the string you want to copy';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');

as you can see - I have the string to copy in hand. But I have to create a temporary hidden DOM element to score the string before copying to the clipboard.

My question is, is there some API to copy to clipboard without needing the DOM at all? something like this:

document.execCommand('copy', 'the string data to put in clipboard');

seems weird that the DOM is necessary to do this. Also, as a side note, this clipboard API is super strange, anyone else agree?

来源:https://stackoverflow.com/questions/48215841/copy-string-to-clipboard-without-using-a-dom-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!