复制的实现

//复制到剪贴板
clipboard(val) {
let result = 0;
let textArea = document.createElement('textArea');
textArea.value = val;
document.body.appendChild(textArea);

if (navigator.userAgent.match(/ipad|iphone/i)) {
let range = document.createRange();
range.selectNodeContents(textArea);
let selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0, 999999);
} else {
textArea.select();
}

try { document.execCommand("Copy") && (result = 1) } catch (err) { }
document.body.removeChild(textArea);

return result;
},

posted @ 2018-12-07 03:40  hh9515  阅读(121)  评论(0)    收藏  举报