问题
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