6

我想允许用户将大量电子邮件地址复制到他们的剪贴板。这个答案似乎是最可靠的方法:

如何在 JavaScript 中复制到剪贴板?

但是,在测试时,我发现它window.prompt() 总是将其中一封电子邮件的一部分替换为....

这是测试数据:

test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com; test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
test@test.com;test@test.com;

这是具有以下测试数据的示例代码:

window.prompt ("To copy emails to clipboard, press: Ctrl+C, then Enter", 
  "test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com; test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;test@test.com;test@test.com;test@test.com;
  test@test.com;test@test.com;");

JSFiddle:http: //jsfiddle.net/MkYDz/

单击上面的链接,将文本复制并粘贴到文本编辑器中,您会看到其中一封电子邮件(大约一半)变为:

test@...om

为什么会这样?

4

3 回答 3

8

window.prompt() 函数在 Chromium 中限制为 2000 个字符。如果输入字符串超出此限制(您的示例有 2059 个字符),则输入字符串将在中间用点标记。这称为省略号

这是一个使用 2000 个字符的演示(我用 XXX 替换了最后三个字符):http: //jsfiddle.net/MkYDz/2/

;test@tesXXX");
于 2013-08-06T19:26:05.110 回答
8

提示的字符限制似乎是MVP所说的原因。有一个更好的解决方法。使用textarea带有电子邮件文本的 a 作为其内容,并使用该select功能选择 textarea 中的整个文本,就像您对提示所做的那样。

例子:

$('#container').append('To copy emails to clipboard, press: Ctrl+C, then Enter <br />
<textarea id="emailsToCopy">'+emailsToCopy+'</textarea>');
$('#emailsToCopy').select().focus();

假设,您使用 jquery。如果不是,您可以select在 textarea DOM 元素上使用函数。

var emailsTxtArea = document.getElementById(emailsToCopy);
emailsTxtArea.select();
emailsTxtArea.focus();
于 2013-08-06T19:39:17.477 回答
2

如果我有更多的声誉点,我会评论 Jitesh 的回答......

如果您不使用 jquery,则不能这样做:

emailsTxtArea.select().focus();

相反,您必须这样做:

emailsTxtArea.select();
emailsTxtArea.focus();

在常规 javascript 中,select 方法不返回对象。

于 2015-05-28T13:55:09.413 回答