Using document.execCommand('copy') in mobile

本小妞迷上赌 提交于 2019-12-10 13:35:26

问题


Is there a way to copy to mobile clipboard? I've been researching for days but haven't found a good solution. Clipboard.js doesn't seem to work on mobile, giving me an error "no support :("

I'm currently using the following function:

function copytext(text) {
    var textField = document.createElement('textarea');
    textField.innerText = text;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand('copy');
    textField.remove();
}

Works like a charm on chrome on my desktop. But on chrome mobile, nothing gets copied.

Is there a solution out there?


回答1:


According to MDN, document.execCommand('copy') is available in the following mobile browsers:

  • Chrome for Android 42+
  • Firefox Mobile (Gecko) 41+

Note that this does not include the iOS Chrome or Firefox, which per-Apple's requirement, both must use the iOS supplied WebKit. Until iOS Safari supports it, iOS Chrome and iOS Firefox probably cannot.

Update:

Safari on iOS 10+ supports cut and copy



来源:https://stackoverflow.com/questions/35996460/using-document-execcommandcopy-in-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!