Copy to Clipboard that also works on Mobile?

让人想犯罪 __ 提交于 2019-11-28 10:08:29

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+.

tlaverdure

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/

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