Cocos Creator JS web平台复制粘贴代码(亲测可用)

Cocos Creator JS web平台复制粘贴代码(亲测可用)

1
webCopyString: function(str){ 2 var input = str; 3 const el = document.createElement('textarea'); 4 el.value = input; 5 el.setAttribute('readonly', ''); 6 el.style.contain = 'strict'; 7 el.style.position = 'absolute'; 8 el.style.left = '-9999px'; 9 el.style.fontSize = '12pt'; // Prevent zooming on iOS 10 11 const selection = getSelection(); 12 var originalRange = false; 13 if (selection.rangeCount > 0) { 14 originalRange = selection.getRangeAt(0); 15 } 16 document.body.appendChild(el); 17 el.select(); 18 el.selectionStart = 0; 19 el.selectionEnd = input.length; 20 21 var success = false; 22 try { 23 success = document.execCommand('copy'); 24 } catch (err) {} 25 26 document.body.removeChild(el); 27 28 if (originalRange) { 29 selection.removeAllRanges(); 30 selection.addRange(originalRange); 31 } 32 33 return success; 34 },
原文地址:https://www.cnblogs.com/luorende/p/9685683.html