粘贴复制

方法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);
});

方法四:

 

posted @ 2019-06-10 14:56  开心小萝卜~  阅读(286)  评论(0编辑  收藏  举报