我研究了一堆答案和文章,它们展示了如何通过 jquery 在按钮单击时复制文本,但没有一个对我有用。我通过 ajax 将一个值附加到 DOM 中,我希望通过单击按钮来复制该值。
所有这些解决方案都可以在 chrome 上运行,如果使用 jsfiddle/codepen,则它们可以在 safari 中运行(版本:13.0.5),但是当通过单独的 html 和 js 文件使用时,它们不能在 safari 中运行,就我而言,无论如何。
首先,我尝试使用位置:绝对和左侧:-99999 进行不可见输入,并使用 jquery 选择其中的文本并使用 execCommand("copy");。这不起作用。
我也尝试过这里提到的答案:Safari 上的 Javascript 复制到剪贴板? https://stackoverflow.com/questions/40147676/javascript-copy-to-clipboard-on-safari
我还尝试了下面提到的两个功能,但没有成功。除了这些功能之外,我还尝试了我能找到的所有 javascript 插件,但这些插件也不起作用。
功能1:
function copy(value, showNotification, notificationText) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val(value).select();
document.execCommand("copy");
$temp.remove();
$(".copyfrom").val(value)
$(".copyfrom").select();
document.execCommand("copy");
//trigger animation---
if (typeof showNotification === 'undefined') {
showNotification = true;
}
if (typeof notificationText === 'undefined') {
notificationText = "Copied to clipboard";
}
var notificationTag = $("div.copy-notification");
if (showNotification && notificationTag.length == 0) {
notificationTag = $("<div/>", { "class": "copy-notification", text: notificationText });
$("body").append(notificationTag);
notificationTag.fadeIn("slow", function () {
setTimeout(function () {
notificationTag.fadeOut("slow", function () {
notificationTag.remove();
});
}, 1000);
});
}
}
功能2:
function copyToClipboard(textToCopy) {
var textArea;
function isOS() {
return navigator.userAgent.match(/ipad|iphone/i);
}
function createTextArea(text) {
textArea = document.createElement('textArea');
textArea.readOnly = true;
textArea.contentEditable = true;
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 copyTo() {
document.execCommand('copy');
document.body.removeChild(textArea);
}
createTextArea(textToCopy);
selectText();
copyTo();
//trigger animation---
if (typeof showNotification === 'undefined') {
showNotification = true;
}
if (typeof notificationText === 'undefined') {
notificationText = "Copied to clipboard";
}
var notificationTag = $("div.copy-notification");
if (showNotification && notificationTag.length == 0) {
notificationTag = $("<div/>", { "class": "copy-notification", text: notificationText });
$("body").append(notificationTag);
notificationTag.fadeIn("slow", function () {
setTimeout(function () {
notificationTag.fadeOut("slow", function () {
notificationTag.remove();
});
}, 1000);
});
}
}
这是我的 ajax 和 html:
$(".fa-link").on("click", function () {
var mlink = $(".boxmlink").attr("href").trim()
var fetchWallID = new XMLHttpRequest()
fetchWallID.open("POST", db, true);
fetchWallID.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
fetchWallID.onload = function () {
if (this.status == 200) {
var url = $(location).attr("href").split("?")
var id = (this.responseText).trim()
var windowurl = url[0].trim()
var finalurl = (windowurl + '?wallid=' + id).trim().replace("#", '')
//copy(finalurl, true)
//copyToClipboard(finalurl)
}
}
fetchWallID.send("mlinkID=" + mlink)
})
html:
<a href="#" class="fa fa-link"></a>