Copy to Clipboard that also works on Mobile?

家住魔仙堡 提交于 2019-12-17 19:21:01

问题


I'm familiar with ZeroClipboard and jquery.copy, which both use Flash. OK, so I get it, for browser security reasons, copying is disallowed and we need Flash. But this means the copying functionality does not work in mobiles (iPhone, Android) or iPad.

Is there any resource or plugin that allows a simple Copy to Clipboard functionality on both modern browsers (include IE7+) and mobile browsers?

Thanks!


回答1:


I just wanted to offer an update, since there have been some recent developments on this front. Modern browsers, except for Safari support copying via JS, using the execCommand() api.

Assuming you build your UI to gracefully degrade to manual copying for Safari, you could implement copy-to-clipboard on the rest of them. Presumably, Safari will include support for this eventually.

Look at https://clipboardjs.com/ and http://www.sitepoint.com/javascript-copy-to-clipboard/ for options.

Browsers supported, as of writing: Chrome 42+, Firefox 41+, IE 9+, Opera 29+.




回答2:


There is not a great solution to do this without using flash. I would just keep it simple and allow your users to copy the text themselves. It's how google serves their analytics code to users to embed on their sites. I'm sure if there was a great way to do it that google would be using it.

Keeping it simple:

$(function(){
    $('.text').click(function(){
        $(this).select();
    });
});

Focus event: http://jsfiddle.net/khXjC/

Click event: http://jsfiddle.net/qjfgoeLm/



来源:https://stackoverflow.com/questions/13015253/copy-to-clipboard-that-also-works-on-mobile

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