0
我試圖從textarea複製文本而不必單擊按鈕。我正在使用execCommand('copy')
,我可以讓它複製文本,但只有當按鈕被點擊,而不是以編程方式。僅使用execCommand在按鈕單擊時複製文本
有沒有辦法做到這一點?
var button = document.querySelector('button');
var area = document.querySelector('textarea');
button.addEventListener("click", function(){
area.select();
var msg = document.execCommand('copy') ? "successful" : "unsuccessful";
console.log("Copy " + msg);
});
button.click(); // Does not copy successfully
<textarea>Text to Copy</textarea>
<button>Copy</button>