通过 Javascript 将换行符粘贴到剪贴板

发布于 2024-09-28 03:10:25 字数 525 浏览 8 评论 0 原文

在提出任何问题之前,我已经对此进行了彻底的研究,并且答案之前尚未发布在这里。

我想用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦巷 2024-10-05 03:10:25

解决方案是使用反引号(```)

alert(`string text line 1
string text line 2`);

供参考:
https://developer.mozilla.org/en-US/docs /Web/JavaScript/Reference/template_strings

The solution is to use back-tick (` `)

alert(`string text line 1
string text line 2`);

For reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

独守阴晴ぅ圆缺 2024-10-05 03:10:25

我建议使用剪贴板以外的其他方法来向用户发送数据。此方法仅适用于 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/

半世蒼涼 2024-10-05 03:10:25

我想我确实找到了解决方案。这有点奇怪,但是嘿,这是针对 IE 的。这是我在 stackoverflow 上找到的修改后的片段。

<body>
    <a href="#" onclick='test("This\nIS\nA\nTEST")'>TEST</a>
    <div id="cb" style="position: absolute; left: -2000px"></div>
</body>
<script>

function test(cText) {
    cText= cText.replace(/\n\r?/g, "<br>");

    // create an editable DIV and append the HTML content you want copied
    var editableDiv = document.getElementById("cb");
    with (editableDiv) {
        contentEditable = true;
    }     
    editableDiv.innerHTML= cText;          

    // select the editable content and copy it to the clipboard
    var r = document.body.createTextRange();
    r.moveToElementText(editableDiv);
    r.select();  
    r.execCommand("Copy");

    // deselect, so the browser doesn't leave the element visibly selected
    r.moveToElementText(document.body);
    r.select();
}

</script>

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.

<body>
    <a href="#" onclick='test("This\nIS\nA\nTEST")'>TEST</a>
    <div id="cb" style="position: absolute; left: -2000px"></div>
</body>
<script>

function test(cText) {
    cText= cText.replace(/\n\r?/g, "<br>");

    // create an editable DIV and append the HTML content you want copied
    var editableDiv = document.getElementById("cb");
    with (editableDiv) {
        contentEditable = true;
    }     
    editableDiv.innerHTML= cText;          

    // select the editable content and copy it to the clipboard
    var r = document.body.createTextRange();
    r.moveToElementText(editableDiv);
    r.select();  
    r.execCommand("Copy");

    // deselect, so the browser doesn't leave the element visibly selected
    r.moveToElementText(document.body);
    r.select();
}

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文