我這裡是調用接口傳回的内容,因為内容在彈出框中顯示,點選彈框按鈕複制内容,看了很多案例都是在有input框的情況下進行複制,是以我這裡就先建立input标簽,複制完後再删除。
function copyText(text) {
var textarea = document.createElement("input");//建立input對象
document.body.appendChild(textarea);//添加元素
textarea.setAttribute("value",text);
textarea.setAttribute("id", "input");
var content= document.getElementById("input");
content.select(); // 選擇對象
document.execCommand("copy");//執行複制
document.body.removeChild(textarea);//删除input對象
};