Javascript 复制保留换行符的按钮

Javascript 复制保留换行符的按钮,javascript,Javascript,我有一些非常基本的Javascript,只需按下按钮即可复制文本。我的问题是它不能保留换行符: <script> function copyToClipboard(element) { var $temp = $("<input>"); $("body").append($temp); $temp.val($(element).text()).select(); document.execCommand("copy"); $temp.remove();

我有一些非常基本的Javascript,只需按下按钮即可复制文本。我的问题是它不能保留换行符:

<script>
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
  $temp.remove();
}
</script>

功能copyToClipboard(元素){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($(element.text()).select();
文件。执行命令(“副本”);
$temp.remove();
}
我真的希望能够添加到上面的脚本,以避免在网站上已经作出巨大的变化

我在其他帖子上看到过一些东西,比如:

post.innerHTML = post.innerHTML.replace(/\n/g, '<br>\n');
post.innerHTML=post.innerHTML.replace(/\n/g,
\n');
这在理论上是可行的(我想),但我在Javascript方面很差劲

有什么建议吗

谢谢

首先,
元素不会保留换行符。您可以改用
元素。由于您的HTML可能包含

元素而不是换行字符,因此我还建议在每个

之前使用jQuery预先添加
\r\n

功能copyToClipboard(元素){
var text=$(元素).clone().find('br').prepend('\r\n').end().text()
元素=$(“”).appendTo('body').val(text).select()
document.execCommand('copy')
元素。删除()
}

键入以编辑此文本


复制到剪贴板
我们调整了copyToClipboard功能,使其能够与我们的应用程序一起工作。变化如下:

  • 将输入更改为textarea,以便传递换行符
  • 将text()函数更改为html(),以便传递html
  • 使用正则表达式将每个HTML br替换为换行符
  • 使用另一个正则表达式剥离剩余的HTML。我们网站中的HTML 应用程序应该只有

    标记,因此简单的正则表达式应该可以工作,并且还可以处理可能存在的奇怪标记
以下是我们的调整功能以及评论:

// Note: original replace used to strip HTML tags from https://stackoverflow.com/questions/5002111/javascript-how-to-strip-html-tags-from-string ReactiveRaven.
// However, replaced closing (>|$) with > as I don't understand why the check for $ is there, as it implies that $ is part of an HTML tag.
// Our requirement highly constrains the HTML, to mainly have <br> and <b> tags, so the general objection to parsing HTML with regex
// as at https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags community wiki is not applicable.
// BTW, that page is very entertaining!
function copyToClipboard(element)
{
    var $temp = $("<textarea>");
    $("body").append($temp);
    var x = $(element).html().trim().replace(/<br>/g, '\n').replace(/<\/?[^>]+>/g, '');
    $temp.val(x).select();
    document.execCommand("copy");
    $temp.remove();
}
//注意:原始替换用于从https://stackoverflow.com/questions/5002111/javascript-how-to-strip-html-tags-from-string 阿维文。
//但是,将结束(>|$)替换为>,因为我不明白为什么会有对$的检查,因为这意味着$是HTML标记的一部分。
//我们的需求高度限制了HTML,主要有
和标记,因此普遍反对使用正则表达式解析HTML //截至https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags 社区维基不适用。 //顺便说一句,那页很有趣! 功能copyToClipboard(元素) { 变量$temp=$(“”); $(“正文”)。追加($temp); var x=$(element.html().trim().replace(/
/g,“\n')。replace(/]+>/g,”); $temp.val(x.select(); 文件。执行命令(“副本”); $temp.remove(); }

顺便说一句,如果有人知道为什么引用页面中的正则表达式的(>|$)被我们改为>,我们会很高兴理解为什么需要我们删除的美元符号。

什么是
元素
类型?输入或文本区域?这应该有助于这项工作的伟大,除了一件事。。。我在文本之间有标签,这似乎也导致它断行。如果出现以下情况,是否可以在上面添加一些可以忽略的内容:这是我希望所有文本都显示在同一行中的文本。在
.find('br')
之前,插入
.find('ref.glowingtext')。remove().end()
太棒了!非常感谢你!