我查看了一些关于如何通过jquery在按钮点击时复制文本的答案和文章,但是它们都没有对我起作用。 我通过ajax将一个值附加到DOM中,希望在单击按钮时将其复制。
所有这些解决方案都适用于Chrome,并且如果使用jsfiddle / codepen,则它们在Safari(版本:13.0.5)中也适用,但是当通过单独的html和js文件使用时,在我的情况下,它们不起作用。
首先,我尝试创建一个具有位置:absolute和left:-99999的不可见输入,并使用jquery选择其中的文本,然后使用execCommand(“copy”)选中文本并复制。 但是这并没有起作用。 我还尝试了在此处提到的答案:Javascript Copy To Clipboard on safari? 我还尝试了下面提到的两个功能,但没有成功。 除了这些函数之外,我还尝试了我能找到的每个javascript插件,但它们也没有起作用。
功能1:
所有这些解决方案都适用于Chrome,并且如果使用jsfiddle / codepen,则它们在Safari(版本:13.0.5)中也适用,但是当通过单独的html和js文件使用时,在我的情况下,它们不起作用。
首先,我尝试创建一个具有位置:absolute和left:-99999的不可见输入,并使用jquery选择其中的文本,然后使用execCommand(“copy”)选中文本并复制。 但是这并没有起作用。 我还尝试了在此处提到的答案: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>