原理: document.execComman('copy')可以復制選中內容到剪貼板,如果成功返回true,失敗返回false
缺點: 並非所有瀏覽器都支持,需要處理異常try...catch...
實現過程: 利用<input>來輔助一下
1.設置input的value為想要復制的內容
oInput.value = "要復制的內容"
2.選中input的所有內容
oInput.select()
3.嘗試復制
try{ document.execCommand('copy'); } catch(e){ console.log('瀏覽器不支持該功能'); }