传递给 window.prompt 的文本被替换为“...”

Posted

技术标签:

【中文标题】传递给 window.prompt 的文本被替换为“...”【英文标题】:Text passed to window.prompt gets replaced with "..." 【发布时间】:2013-08-07 23:03:04 【问题描述】:

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

How do I copy to the clipboard in 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

为什么会这样?

【问题讨论】:

您使用的是哪个浏览器?工作表。 我可以用 Chrome 重现这个。 在 Chrome 28、OSX 10.7 上为我工作。但是如果它在某些浏览器中不起作用,则必须使用自己的自制模态和文本区域。 @bfavaretto 我认为这是首选方式! 这在 IE 中运行良好,似乎只是 Chrome 中的问题? code.google.com/p/chromium/issues/detail?id=58144 【参考方案1】:

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

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

;test@tesXXX");

【讨论】:

【参考方案2】:

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

例子:

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

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

var emailsTxtArea = document.getElementById(emailsToCopy);
emailsTxtArea.select();
emailsTxtArea.focus();

【讨论】:

【参考方案3】:

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

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

emailsTxtArea.select().focus();

相反,您必须这样做:

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

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

【讨论】:

以上是关于传递给 window.prompt 的文本被替换为“...”的主要内容,如果未能解决你的问题,请参考以下文章

在单元格javascript中设置类型文本

Vuex - 传递给操作的参数丢失。替换为当前状态

如何将原始innerHTML 传递给php?

替换Groovy XML节点会导致后续findAll调用出现问题

如何在 RegEx 替换中将 RegEx 令牌传递给 PowerShell 子表达式?

VBscript 替换文本但添加空行