方法1:? ?
方法二:
方法三:
// 第三種 ios 設備和 android設備均正常,但是pc端沒有
//定義函數
window.Clipboard = (function(window, document, navigator) {
var textArea,
copy;
// 判斷是不是ios端
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
//創建文本元素
function createTextArea(text) {
console.log(text,"text");
textArea = document.createElement('textArea');
console.log(textArea,"textArea");
textArea.innerHTML = text;
textArea.value = text;
console.log(textArea.value,"textArea.value");
document.body.appendChild(textArea);
}
//選擇內容
function selectText() {
var range,
selection;
if (isOS()) {
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}
}
//復制到剪貼板
function copyToClipboard() {
try{
if(document.execCommand("Copy")){
Toast("復制成功!",1000);
}else{
Toast("復制失敗!請手動復制!",1000);
}
}catch(err){
Toast("復制錯誤!請手動復制!",1000);
}
document.body.removeChild(textArea);
}
copy = function(text) {
createTextArea(text);
selectText(text);
copyToClipboard(text);
};
return {
copy: copy
};
})(window, document, navigator);
//使用函數
$("#copy").on("click",function(){
var val = $("#textAreas").val();
console.log("val",val)
Clipboard.copy(val);
});
方法四:
?