javascript 如何复制警告框的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17591559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:54:16  来源:igfitidea点击:

How to copy text of alert box

javascript

提问by Stack Over

I write the path of a document into the alert box via using below code.

我通过使用以下代码将文档的路径写入警报框中。

var oArg = new Object();
oArg.Document = $(t).attr("path") + str + "/" + $(t).attr("name");
alert(oArg.Document);

Assume that message is : "documents/files/img/stack.jpg"

假设消息是:“ documents/files/img/stack.jpg

I only want to copy this text with a button. For Chrome Ctrl + C is ok for it but for IE, Ctrl + C copies everything at the alertbox.

我只想用一个按钮复制这个文本。对于 Chrome Ctrl + C 是可以的,但对于 IE,Ctrl + C 会复制警报框中的所有内容。

How can I copy only the message with using a button?

如何使用按钮仅复制消息?

Ctrl + C works like below:

Ctrl + C 的工作方式如下:

  • Chrome - Works perfectly.
  • Internet Explorer - Works, but you get extra text. The caption and OK button text is also copied, along with a bunch of dashes. This is almost never what you want.
  • Firefox - Doesn't work at all You must select the text before you can copy it.
  • Chrome - 完美运行。
  • Internet Explorer - 有效,但您会获得额外的文本。标题和 OK 按钮文本以及一堆破折号也被复制。这几乎永远不是您想要的。
  • Firefox - 根本不起作用 您必须先选择文本,然后才能复制它。

回答by Eric

What you can do is to prompt the user with the text and ask them to copy it. As such:

您可以做的是用文本提示用户并要求他们复制它。像这样:

prompt("Copy to clipboard: Ctrl+C, Enter", oArg.Document);

Because if you supply a text to the prompt it automatically gets selected. Does this suit you?

因为如果您向提示提供文本,它会自动被选中。这适合你吗?

回答by Sujeet malvi

When ever the alert message box appears with the text message , just press CTRL+ Cand your message will be copied, then you can paste it anywhere you want , no need to write it or using any other method to get the text.

当警告消息框与文本消息一起出现时,只需按CTRL+C 即可复制您的消息,然后您可以将其粘贴到任何您想要的位置,无需编写或使用任何其他方法来获取文本。

回答by Venryx

I've found that, in Chrome at least, the alert box text is in fact selectable -- when the text does not contain any line-breaks(ie. "\n").

我发现,至少在 Chrome 中,警报框文本实际上是可选择的——当文本不包含任何换行符(即“\n”)时。

I don't know why this is the case, but I've had to use this trick before for quick selection prompts. (you could also use prompt("please press ctrl+c to copy the text below", "text to copy"), but that usually looks worse)

我不知道为什么会这样,但我之前不得不使用这个技巧来快速选择提示。(您也可以使用prompt("please press ctrl+c to copy the text below", "text to copy"),但通常看起来更糟)