Javascript的复制到剪贴板上safari? [英] Javascript Copy To Clipboard on safari?

查看:103
本文介绍了Javascript的复制到剪贴板上safari?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是重复的问题,但我没有找到解决方案。



我试图复制按钮单击文本。它的工作在铬,Mozilla(在Windows和Mac上工作,但不是在Linux上)。而且它在safari上没有工作。

我正在使用 document.execCommand(copy)命令进行复制。

Safari是否支持这个命令?

这将支持所有浏览器?

解决方案

请检查我的解决方案。 b $ b

它适用于Safari(在iPhone 7和iPad上测试)以及其他浏览器。

  window.Clipboard =(function(window,document,navigator){
var textArea,
copy;

function isOS(){
return navigator.userAgent.match(/ ipad (textArea);
textArea.value = text;}


函数createTextArea(text){
textArea = document.createElement('textArea');
textArea.value = text;
document.body.appendChild(textArea);
}

函数selectText(){
var range,
selection;

if(isOS()){
range = document.createRange();
range.selectNodeContents(textArea);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textArea.setSelectionRange(0,999999);
} else {
textArea.select();



copyToClipboard(){
document.execCommand('copy');
document.body.removeChild(textArea);


copy = function(text){
createTextArea(text);
selectText();
copyToClipboard();
};

return {
copy:copy
};
})(window,document,navigator);

//如何使用
Clipboard.copy('text to be copied');

https://gist.github.com/rproenca/64781c6a1329b48a455b645d361a9aa3
https://fiddle.jshell.net/k9ejqmqt/1/



希望可以帮到你。



问候。


It may be duplicate question but i didnt find the solution for this.

I am trying to copy text on button click. Its working on chrome, mozilla(working on on windows and mac but not on linux). And its not working on safari.

I am using document.execCommand("copy") command for copy.

Is safari support this command?

Is there any way which will supports to all browsers?

解决方案

Please check my solution.

It works on Safari (tested on iPhone 7 and iPad) and on other browsers.

window.Clipboard = (function(window, document, navigator) {
    var textArea,
        copy;

    function isOS() {
        return navigator.userAgent.match(/ipad|iphone/i);
    }

    function createTextArea(text) {
        textArea = document.createElement('textArea');
        textArea.value = text;
        document.body.appendChild(textArea);
    }

    function selectText() {
        var range,
            selection;

        if (isOS()) {
            range = document.createRange();
            range.selectNodeContents(textArea);
            selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
            textArea.setSelectionRange(0, 999999);
        } else {
            textArea.select();
        }
    }

    function copyToClipboard() {        
        document.execCommand('copy');
        document.body.removeChild(textArea);
    }

    copy = function(text) {
        createTextArea(text);
        selectText();
        copyToClipboard();
    };

    return {
        copy: copy
    };
})(window, document, navigator);

// How to use
Clipboard.copy('text to be copied');

https://gist.github.com/rproenca/64781c6a1329b48a455b645d361a9aa3 https://fiddle.jshell.net/k9ejqmqt/1/

Hope that helps you.

Regards.

这篇关于Javascript的复制到剪贴板上safari?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆