我有一个经典的ASP页面,该页面从数据库中提取记录并在表中显示结果。列之一显示基于所显示的记录集的电子邮件。我想在复制该列中所有电子邮件的页面上添加“复制到剪贴板”按钮。
这是当前用于遍历记录集的脚本。
RESPONSE.WRITE "<tr>"& vbCrlf
RESPONSE.WRITE "<td>FULL NAME</td>"& vbCrlf
RESPONSE.WRITE "<td>EMAIL</td>"& vbCrlf
RESPONSE.WRITE "</tr>"& vbCrlf
IF NOT rsMembers.EOF AND NOT rsMembers.BOF THEN
DO WHILE NOT rsMembers.EOF
RESPONSE.WRITE "<tr>"& vbCrlf
RESPONSE.WRITE "<td>" & rsMembers("First Name") & " " & rsMembers("Last Name") & "</td>"& vbCrlf
RESPONSE.WRITE "<td><a href='mailto:"&rsMembers("Email")&"'>" & rsMembers("Email") & "</a></td>"& vbCrlf
RESPONSE.WRITE "</tr>"& vbCrlf
rsMembers.MOVENEXT
LOOP
END IF
我最初的尝试是在循环中为每个电子邮件记录包括一个隐藏字段,并通过向其添加数字或其他方式使该字段的ID唯一。然后在javascript循环中遍历所有可能的ID。
RESPONSE.WRITE "<tr>"& vbCrlf
RESPONSE.WRITE "<td>FULL NAME</td>"& vbCrlf
RESPONSE.WRITE "<td>EMAIL</td>"& vbCrlf
RESPONSE.WRITE "</tr>"& vbCrlf
IF NOT rsMembers.EOF AND NOT rsMembers.BOF THEN
DO WHILE NOT rsMembers.EOF
RESPONSE.WRITE "<tr>"& vbCrlf
RESPONSE.WRITE "<td>" & rsMembers("First Name") & " " & rsMembers("Last Name") & "</td>"& vbCrlf
RESPONSE.WRITE "<td><a href='mailto:"&rsMembers("Email")&"'>" & rsMembers("Email") & "</a><input type='hidden' value='"& rsMembers("Email") &"' id='myInput'></td>"& vbCrlf
RESPONSE.WRITE "</tr>"& vbCrlf
rsMembers.MOVENEXT
LOOP
END IF
Java
<script>
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
</script>
然后添加一个按钮进行复制:
<button onclick="myFunction()">Copy text</button>
不确定是否有办法将所有电子邮件结果放入并排列,然后将它们写到一个隐藏字段中。例如
<input type="hidden" value="DB Array of emails with a comma between each" id="myInput">
答案 0 :(得分:1)
这是一个jQuery解决方案。
“全部复制”按钮触发了点击事件。通过使用“电子邮件”类检索任何元素的文本值来收集电子邮件地址,然后将其传递到名为“ copyToClipboard”的函数,该函数创建一个临时的隐藏文本区域,以从中选择和复制电子邮件值。它应该可以在所有浏览器中使用。
编辑:我已经更新了代码,使您可以从任何列复制数据。您只需要指定目标类并将其作为数据属性传递即可。您可能只需要指定一个列号并查找嵌套的表数据,而不必分配类,但是不幸的是,这超出了我的JavaScript知识,加上emails列中的href链接将使操作变得很棘手。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Database Results</title>
</head>
<body>
<table width="0%" border="2" cellspacing="3" cellpadding="3">
<tbody>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Names - <button class="CopyColumn" data-target=".names">Copy All</button></th>
<th>Emails - <button class="CopyColumn" data-target=".emails" data-separator="; ">Copy All</button></th>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name One</td>
<td><a href="mailto:email1@email.com" class="emails">email1@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Two</td>
<td><a href="mailto:email2@email.com" class="emails">email2@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Three</td>
<td><a href="mailto:email3@email.com" class="emails">email3@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Four</td>
<td><a href="mailto:email4@email.com" class="emails">email4@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Five</td>
<td><a href="mailto:email5@email.com" class="emails">email5@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Six</td>
<td><a href="mailto:email6@email.com" class="emails">email6@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Seven</td>
<td><a href="mailto:email7@email.com" class="emails">email7@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Eight</td>
<td><a href="mailto:email8@email.com" class="emails">email8@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Nine</td>
<td><a href="mailto:email9@email.com" class="emails">email9@email.com</a></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td class="names">Name Ten</td>
<td><a href="mailto:email10@email.com" class="emails">email10@email.com</a></td>
</tr>
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".CopyColumn").click(function(){
var btn = $(this);
// Disable the button whilst the clipboard copy is performed
btn.prop("disabled", true);
var colData = "";
// Use a line break to seperate the column data if no separator is specified
var colSeparator = (btn.data("separator")===undefined) ? "\n" : btn.data("separator");
// Loop through all elements with the target class
$(btn.data("target")).each(function() {
// Collect the column data and add the separator
colData += $(this).text() + colSeparator;
});
// Copy the column data to the clipboard
copyToClipboard(colData.trim());
// Make a copy of the button text
var btn_txt = btn.html();
// Change the button text to "Copied"
btn.html("Copied");
// Revert the button text after 1.5 seconds
setTimeout(function(){
btn.html(btn_txt);
// Enable the button
btn.prop("disabled", false);
},1500);
});
});
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
</script>
</body>
</html>
JSfiddle:https://jsfiddle.net/j4r9sxwt/
您唯一需要对ASP代码进行的更改是:
RESPONSE.WRITE "<td><a href='mailto:"&rsMembers("Email")&"' class='emails'>" & rsMembers("Email") & "</a></td>"& vbCrlf