2010-08-06 54 views
2


我正在尋找一個可以將TextArea中的內容複製到剪貼板的JavaScript函數。在微軟平臺上,該功能可以正常工作,但是當我切換到非微軟平臺如FireFox或Safari時,它失敗了。
我提到這個link的功能。使用JavaScript在非Microsoft平臺上將文本從TextArea複製到剪貼板


如果有人知道這個解決方案,請幫助我。
在此先感謝。

+0

重複的:http://stackoverflow.com/questions/127040/copy-put-text-on-the-clipboard-with-firefox-safari-and-chrome和http://stackoverflow.com/questions/ 2072026/copy-to-clipboard-not-working-on-firefox – RoToRa 2010-08-06 08:59:53

+0

但這些問題提供並接受的解決方案不起作用。所以我想重新發布它以獲得更好的解決方案。 – 2010-08-07 10:43:53

回答

0

爲當用戶點擊文本區域複製textarea的內容到剪貼板純JavaScript解決方案:

<script> 

function copySelectionText(){ 
    var copysuccess // var to check whether execCommand successfully executed 
    try{ 
     copysuccess = document.execCommand("copy") // run command to copy selected text to clipboard 
    } catch(e){ 
     copysuccess = false 
    } 
    return copysuccess 
} 

function copyfieldvalue(e, id){ 
    var field = document.getElementById(id) 
    field.select() 
    var copysuccess = copySelectionText() 
} 

var bio = document.getElementById('mybio') 
bio.addEventListener('mouseup', function(e){ 
    copyfieldvalue(e, 'mybio') 
    var copysuccess = copySelectionText() // copy user selected text to clipboard 
}, false) 

</script> 

注:如果要複製的textarea的內容只有部分到剪貼板,教程Reading and copying selected text to clipboard using JavaScript有更多的信息。

相關問題