gpt4 book ai didi

javascript - 是否可以使用 Javascript 复制到剪贴板?

转载 作者:行者123 更新时间:2023-12-02 22:48:29 25 4
gpt4 key购买 nike

我正在开发一个共享点 Web 部件,它有一个按钮,可以从同一页面上的不同文本框中提取元素,并将它们整理到一个字符串中,然后复制到用户的剪贴板,以便他们可以快速地将通信组合在一起问题。到目前为止,我有下面的代码,但它实际上并没有复制任何内容。我已经通过 JSHint 运行了它,并且没有出现任何问题,但我在函数底部找到了用于复制文本的代码,该代码来自有关与剪贴板 API 交互的教程,以了解如何从文本框进行复制,因此为什么我将所有内容添加到 smsToSend 文本区域。需要注意的是,如果存在一个全新的问题并且以前没有发出过,那么事件更新始终是“我们正在调查该问题”,因为它会自动放入该字段,这就是我测试的原因它,因为新的和更新的通信都会将“打开”作为事件状态。

function generateSMS(){
var issueTitle = document.getElementById("incidentTitle");
var advisorImpact = document.getElementById("advisorImpact");
var incidentUpdate = document.getElementById("incidentUpdate");
var incidentStatus = document.getElementById("incidentState");
var startTime = document.getElementById("startTime");
var endTime = document.getElementById("endTime");
var smsToSend = document.createElement('textarea');
var incidentPriority = document.getElementById("incidentPriority");
var incidentBrand = "TechTeams";
var systemImpacted = document.getElementById("systemImpacted");
var incidentReference = document.getElementById("incidentReference");

if (incidentStatus != "Closed"){
if (incidentUpdate == "We are investigating this issue"){
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT ISSUE: " + systemImpacted + ": " + issueTitle + ". " + advisorImpact + ": " + incidentReference;
}
else {
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT UPDATE: " + systemImpacted + ": " + incidentUpdate + ": " + incidentReference;
}
}
else{
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT RESOLVED: " + systemImpacted + ": " + incidentUpdate + ": Start: " + startTime + " End: " + endTime + " Reference: " + incidentReference;
}

smsToSend.setAttribute('readonly','');
smsToSend.style = {position: 'absolute', left: '-9999px'};
document.body.appendChild(smsToSend);
smsToSend.select();
document.execCommand('copy');
document.body.removeChild(smsToSend);
}

最佳答案

您可以使用 js 轻松复制到剪贴板,如下所示:

function CopyToClipboard(text) {
/* Get the text field */
var copyText = document.getElementById("elementId").textContent; //here you get the text
var dummy = $('<textarea>').val(copyText).appendTo('body').select();
document.execCommand('copy');//here the text gets copyed
alert("Text copyed to clipboard!");
$(dummy).remove();// here you remove the dummy that has been created previously
}

关于javascript - 是否可以使用 Javascript 复制到剪贴板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58284246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com