我正在开发一个共享点Web部件,该部件具有一个按钮,可从同一页面上的不同文本框中提取元素,并将它们整理到一个字符串中,然后复制到用户的剪贴板中,以便他们可以快速地为问题。到目前为止,我有下面的代码,但实际上并没有复制任何内容。我已经通过JSHint运行了它,但没有出现任何问题,但是我从有关与剪贴板API交互的教程(如何从文本框中复制)的教程中选取了用于复制文本的函数底部的代码。为什么我将所有内容添加到smsToSend文本区域。给人的提示是,如果存在一个全新的问题并且之前从未发出过,那么事件更新始终是“我们正在调查该问题”,因为该问题会自动放入现场,这就是我针对此问题进行测试的原因它,因为新通讯和更新通讯都将“ Open”作为事件状态。
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);
}
答案 0 :(得分:1)
您可以像这样轻松地将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
}