Javascript 抄袭。下面是我的答案,它正在与用户交互。检查我的回答,但是,为什么这段代码在ajax调用之外成功运行。如果我在cardnumber.Hi-Serg中添加硬代码值,则copied的值为“false”。如何使它适用于发送的文本而不是元素,因为我想动态复制它,我
Javascript 抄袭。下面是我的答案,它正在与用户交互。检查我的回答,但是,为什么这段代码在ajax调用之外成功运行。如果我在cardnumber.Hi-Serg中添加硬代码值,则copied的值为“false”。如何使它适用于发送的文本而不是元素,因为我想动态复制它,我,javascript,jquery,Javascript,Jquery,抄袭。下面是我的答案,它正在与用户交互。检查我的回答,但是,为什么这段代码在ajax调用之外成功运行。如果我在cardnumber.Hi-Serg中添加硬代码值,则copied的值为“false”。如何使它适用于发送的文本而不是元素,因为我想动态复制它,我不想将它存储在任何元素中。@user27466我仍在研究它,但我相信这是一个安全问题。将用户的内容复制到剪贴板可能已经导致恶意行为。完全不可见地这样做甚至更糟糕,但是我不能说是否可以做到。@user2746466我认为如果你在css中显示新元素
抄袭。下面是我的答案,它正在与用户交互。检查我的回答,但是,为什么这段代码在ajax调用之外成功运行。如果我在cardnumber.Hi-Serg中添加硬代码值,则copied的值为“false”。如何使它适用于发送的文本而不是元素,因为我想动态复制它,我不想将它存储在任何元素中。@user27466我仍在研究它,但我相信这是一个安全问题。将用户的内容复制到剪贴板可能已经导致恶意行为。完全不可见地这样做甚至更糟糕,但是我不能说是否可以做到。@user2746466我认为如果你在css中显示新元素
display:none
,你至少可以让用户觉得你不是为了这个而创建和删除一个元素。嗨,Gangadhar,如果你在javascript文件中尝试这个,它不起作用。它在开发者工具栏colsole中运行良好:(尝试在ajax调用之前创建一个隐藏字段,然后在ajax成功函数中设置值,选择元素,然后执行复制命令仍然不走运!:(嘿,没有jQuery它可以工作,如果我添加jQuery它就会停止工作。我无法删除jQuery。)
$temp.val(data.CardNumber);
$temp.select();
copied = document.execCommand("copy");
$temp.remove();
if(copied){
alert('copied successfully');
}else{
alert('something went wrong');
}
var response;
// recursively using setTimeout to wait for response
var recCopy = function (){
if(response){
copy(response);
return;
}
else {
setTimeout(recCopy,2000); // for e.g. 2ms
}
}
function copy(value) {
var tempInput = document.createElement("input");
tempInput.style = "position: absolute; left: -1000px; top: -1000px";
tempInput.value = value;
document.body.appendChild(tempInput);
tempInput.select();
try {
var successful = document.execCommand('copy',false,null);
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
alert('Oops, unable to copy to clipboard');
}
document.body.removeChild(tempInput);
}
$('.copyToClipboard').on('mousedown',function (e){
$.ajax({
url: "https://www.example.com/xyz",
data: {
formData :formData
},
type : 'post',
success: function (data) {
response = data; // on success, update the response
}
})
});
$('.copyToClipboard').on('click',function (e){
recCopy();
});
$("#jq-copy-txt").on('mouseenter', function() {
$.ajax({
url: 'url',
method: 'GET'
}).then(function(data) {
let copyFrom = document.getElementById("jq-cpy-txt-area");
document.body.appendChild(copyFrom);
copyFrom .textContent = data.title;
});
});
$("#jq-copy-txt").on('click', function() {
var fn = function() {
let copyFrom = document.getElementsByTagName("textarea")[0];
copyFrom.select();
document.execCommand("copy");
};
setTimeout(fn, 1000);});
$link.on("mousedown", function() {
var url = $(this).data("url");
var $temp = $("<input id='copy_container' style='position:fixed;left:-200px;'>");
$.ajax({
url: url,
dataType: "json",
success: function (data) {
$("body").append($temp);
$temp.val(data.text);
}
})
})
$link.on("click", function() {
setTimeout(function() {
var $input = $("input#copy_container");
if ($input.length && $input.val().length > 0) {
$input.select();
document.execCommand("copy");
$input.remove();
}
}, 100)
})
$('#btn').on('click', function(e){
var url = 'Your-link.com';
var $temp = $("<input id='copy_container'
style='position:fixed;left:-200px;'>");
$.ajax({
type: "POST",
url: url,
success: function(res) {
$("body").append($temp);
$temp.val(res);
},
error: function() {
//handle error and do something
}
});
setTimeout(function() {
var $input = $("input#copy_container");
if ($input.length && $input.val().length > 0) {
$input.select();
document.execCommand("copy");
$input.remove();
}
}, 500)
});
<div>
<div>
<label>Content</label>
</div>
<div>
<button type="button" onclick="fnGenerate()">Retrieve and Copy To Clipboard</button>
</div>
<div>
<button type="button" onclick="fnCopy()">Copy To Clipboard</button>
</div>
<div>
<div>
<textarea cols="50" rows="8" id="Content"></textarea>
</div>
</div>
</div>
function fnGenerate() {
$.ajax({
type: 'POST',
dataType: 'json',
............
............
url: 'someUrl',
success: function (data, textStatus, xhr) {
$('#Content').val(data);
$('#Content').select();
document.execCommand('copy');
},
error: function (xhr) {
//Do Something to handle error
var error = xhr.responseText;
}
});
}
function fnCopy() {
$("#Content").select();
document.execCommand('copy');
}