好吧,所以我试图结合这两个功能,将一个段落迅速变成文本区域,以便将其复制到剪贴板,然后使其消失。另一个使复制到剪贴板的操作实际上在移动设备(特别是iOS设备)上有效。
// Original copy to clipboard function
function copyToClipboard(elementId) {
var text = $(elementId).clone().find("br").prepend("\r\n").end().text();
element = $("<textarea>").appendTo("body").val(text).select();
document.execCommand("copy");
element.remove();
}
可在以下位置找到适用于iOS的功能:Make clipboard copy-paste work on iphone devices
//My attempt to marry the two...
var copyToClipboard = function(element) {
var text = $(element).clone().find("br").prepend("\r\n").end().text();
element = $("<textarea>").appendTo("body").val(text).select();
var textarea = document.getElementById(element);
var isiOSDevice = navigator.userAgent.match(/ipad|iphone/i);
if (isiOSDevice) {
var editable = textarea.contentEditable;
var readOnly = textarea.readOnly;
textarea.contentEditable = true;
textarea.readOnly = false;
var range = document.createRange();
range.selectNodeContents(textarea);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
textarea.setSelectionRange(0, 999999);
textarea.contentEditable = editable;
textarea.readOnly = readOnly;
} else {
textarea.select();
}
document.execCommand(\'copy\');
element.remove();
}
我要复制的元素。
<!-- test from example -->
<textarea id="foo">TEst</textarea>
<button onclick="copyToClipboard(\'foo\')">Copy text</button>
<!-- what I'm actually trying to copy -->
<p contenteditable="true" id="spiffy_social_copy">Title</br>Description</br>#hashtags</p>
<button onclick="copyToClipboard(\'spiffy_social_copy\')"></button>