在提出任何问题之前,我已经对此进行了彻底的研究,并且答案之前尚未发布在这里。
我想用 Javascript 将一些纯文本配置文本发送到剪贴板。该文本将包含多个命令,每行一个命令,以便用户可以使用文本编辑器(最常见的是、记事本.exe)。
我尝试了以下操作:
var cCRLF = String.fromCharCode(10,13);
var cText = 'This is command line 1'+cCRLF;
cText += 'This is command line 2'+cCRLF;
cText += 'This is command line 3'+cCRLF;
cText += 'This is command line 4';
window.clipboardData.setData('Text', cText);
但是当我执行并粘贴到记事本中时,
我没有得到单独的行,并且
行返回字符(cCRLF)不可见
(出现那个讨厌的小框字符)。
有人有解决方案吗?
Before any asks, I did research this thoroughly and the answer has not been post here previously.
I want to send some plain text configuration text to the clipboard with Javascript. The text will consist of multiple commands, one command per line, so that the user may then past into a configuration file on his PC (call it "myconfig.ini
") using a text editor (most commonly, Notepad.exe).
I tried the following:
var cCRLF = String.fromCharCode(10,13);
var cText = 'This is command line 1'+cCRLF;
cText += 'This is command line 2'+cCRLF;
cText += 'This is command line 3'+cCRLF;
cText += 'This is command line 4';
window.clipboardData.setData('Text', cText);
but when I execute and paste into notepad,
I don't get individual lines and
the line return character (cCRLF) is not viewable
(that nasty little box character appears).
Does someone have a solution for this?
发布评论
评论(3)
解决方案是使用反引号(```)
供参考:
https://developer.mozilla.org/en-US/docs /Web/JavaScript/Reference/template_strings
The solution is to use back-tick (` `)
For reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
我建议使用剪贴板以外的其他方法来向用户发送数据。此方法仅适用于 IE,并且可以禁用(较新的 IE 版本首先提示): 在 javascript 中将剪贴板数据作为数组获取
CSS 弹出框(用户可以从自己复制)可能是一个更好的(且跨平台的)解决方案。这可能会有所帮助: http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
I would suggest using some other method than the clipboard for sending data to the user. This method only works in IE and can be disabled (and newer IE versions prompt first): Get clipboard data as array in javascript
A CSS popup box (which the user can copy from themselves) would probably be a nicer (and cross-platform) solution. This might help: http://www.pat-burt.com/web-development/how-to-do-a-css-popup-without-opening-a-new-window/
我想我确实找到了解决方案。这有点奇怪,但是嘿,这是针对 IE 的。这是我在 stackoverflow 上找到的修改后的片段。
I think i did find a solution. It's a bit weird, but hey, it's for IE. It's a modified snippet I found on stackoverflow.