javascript 使剪贴板复制粘贴在 iphone 设备上工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32851485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:49:32  来源:igfitidea点击:

Make clipboard copy-paste work on iphone devices

javascriptjqueryhtmliphoneclipboard

提问by Anna Gabrielyan

I have web application, which is mostly designed to be run on mobile devices. I have one button, which will copy to device clipboard the passed text. I am using javascript for that. My code is working great on all mobile devices, except for iphone and ipad. Anybody knows what can be the problem? Here is my code:

我有 Web 应用程序,它主要设计为在移动设备上运行。我有一个按钮,它会将传递的文本复制到设备剪贴板。我为此使用了 javascript。我的代码在所有移动设备上都运行良好,除了 iphone 和 ipad。有谁知道可能是什么问题?这是我的代码:

CopyToClipboard = function(text, fallback){
    var $t = $('<textarea />');
    $t.val(text).appendTo('body');
    $t.select();
    document.execCommand('copy');
    $t.remove();
    return true;   
};

I have also tried to go this way, but no result, still not working on iphone

我也尝试过这种方式,但没有结果,仍然无法在 iphone 上工作

function detectIE() {
    var ua = window.navigator.userAgent;

    var msie = ua.indexOf('MSIE ');
    if (msie > 0) {
        // IE 10 or older => return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
    }

    var trident = ua.indexOf('Trident/');
    if (trident > 0) {
        // IE 11 => return version number
        var rv = ua.indexOf('rv:');
        return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
    }

    var edge = ua.indexOf('Edge/');
    if (edge > 0) {
        // IE 12 => return version number
        return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
    }

    // other browser
    return false;
}
function copytext(text) {
    if (detectIE()) {
        window.clipboardData.setData('Text', text);
    }
    var textField = document.createElement('textarea');
    textField.innerText = text;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand('copy');
    window.clipboardData.setData('Text', copytext);
    textField.remove();
}

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

回答by Rikard Askel?f

Try this. Works for me.

试试这个。对我来说有效。

var copy = function(elementId) {

 var input = document.getElementById(elementId);
 var isiOSDevice = navigator.userAgent.match(/ipad|iphone/i);

 if (isiOSDevice) {
   
  var editable = input.contentEditable;
  var readOnly = input.readOnly;

  input.contentEditable = true;
  input.readOnly = false;

  var range = document.createRange();
  range.selectNodeContents(input);

  var selection = window.getSelection();
  selection.removeAllRanges();
  selection.addRange(range);

  input.setSelectionRange(0, 999999);
  input.contentEditable = editable;
  input.readOnly = readOnly;

 } else {
   input.select();
 }

 document.execCommand('copy');
}
<input type="text" id="foo" value="text to copy" />
<button onclick="copy('foo')">Copy text</button>

回答by Cube.

According to CanIUse, Safari on iOS doesn't support document.execCommand('copy'), probably because of security reasons.

根据CanIUse 的说法,iOS 上的 Safari 不支持document.execCommand('copy'),可能是出于安全原因。