最新js實現複製貼上功能例項
阿新 • • 發佈:2018-11-14
功能:實現滑鼠點選複製內容
描述:使用與手機網站、pc等端。
實現點選複製貼上功能的程式碼例項:
function copy_text(text) { if(text.indexOf('-') !== -1) { var arr = text.split('-'); text = arr[0] + arr[1]; } var textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = '0'; textArea.style.left = '0'; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = '0'; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? '成功複製到剪貼簿' : '該瀏覽器不支援點選複製到剪貼簿'; alert(msg); } catch (err) { alert('該瀏覽器不支援點選複製到剪貼簿'); } document.body.removeChild(textArea); }