copy to clipboard - not working in FF,Chrome

前端 未结 6 1984
小蘑菇
小蘑菇 2021-02-04 13:17

I am using below mentioned javascript to copy the text to clipboard. Its working in IE, but not working in Firefox and Chrome.

Please advice me,What is wrong?

         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 13:46

    I had this same problem with Chrome and other browsers recently. However, recently, I found this code works in a contenteditable field in certain browsers:

    clipboard = e.originalEvent.clipboardData;
    clipboard.setData('text/plain', plainData);
    clipboard.setData('text/html', htmlData);
    

    NOTE: e in this case is the copy and/or cut event. This event fires and is retrievable in an onCopy() or onCut() action.

    This code is confirmed to work in the latest versions of the following browsers:

    • Chrome (PCs/Macs and Android)
    • Android 4.4+ WebView (as long as you update from the Play Store) -> good news for Android Devs
    • Firefox
    • Safari (Mac only)

    Internet Explorer seems to work with window.clipboardData.setData instead, but keep in mind that the IE clipboard will only accept 'text' and 'url' data.

    While the following browsers can access the system clipboard object, these are unable to set data into the clipboard using clipboard.setData:

    • MS Edge
      • gives an UntrustedDragDrop object into the clipboard instead...
      • also, setData returns true... when it doesn't work. setData returns undefined in all other browsers
    • Android WebView -> below 4.4
    • iOS Safari and WebView - yay iOS!

提交回复
热议问题