我想将文本复制到剪贴板,但是要换行。
问题:
如果您点击代码段中的按钮并粘贴到记事本中,这就是您要获得的内容:
名称:testSurname:testEmail:test@gmail.com地址:testCity:testCountry:nullAd类别:testPlan:null网站:公司名称:testΜήνυμα:test
我想要的是什么:
如果可能,我希望在换行符中复制文本。与复制时相同:
姓名:测试
姓:测试
电子邮件:test@gmail.com
...
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
$( "#FailCopy" ).click(function() {
alert("Well done! div #error-details has been copy to your clipboard, now paste it in the notepad or email!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--text that i want to copy-->
<h2>Div #error-details: the text I want to copy to clipboard:</h2>
<er id="error-details">Name: test<br>Surname: test<br>Email: test@gmail.com<br>Address: test<br>City: test<br>Country: null<br>Ad Category: test<br>Plan: null<br>Website: <br>Company name: test<br>Μήνυμα: test</er>
<br><br>
<button id="FailCopy" type="button"
onclick="copyToClipboard('er#error-details')">Copy div to clipboard</button>
我还尝试将<br>
替换为\n
和\r\n
,将以下css规则添加到我的div:white-space:pre-wrap;
,但没有任何成功迹象。
答案 0 :(得分:18)
您的代码存在一些问题。
代码中的第一个问题是$(element).text()
jquery text()从html中删除代码,包括<br>
标记。因此,剪贴板文本中没有换行符,因为所有html换行都被删除了..所以无需替换。
如果您想将<br>
作为换行符,则需要使用.html()
并手动解析文字。
第二个问题是您使用<input>
标记。 <input>
标记只有单行,所以你不能有任何换行符。您需要使用<textarea>
进行转换。
最后一个问题是,您还应该为Windows用户使用\r\n
。
我使用正常版本更新了您的代码段。
function copyToClipboard(element) {
var $temp = $("<textarea>");
var brRegex = /<br\s*[\/]?>/gi;
$("body").append($temp);
$temp.val($(element).html().replace(brRegex, "\r\n")).select();
document.execCommand("copy");
$temp.remove();
}
$( "#FailCopy" ).click(function() {
alert("Well done! div #error-details has been copy to your clipboard, now paste it in the notepad or email!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--text that i want to copy-->
<h2>Div #error-details: the text I want to copy to clipboard:</h2>
<er id="error-details">Name: test<br>Surname: test<br>Email: test@gmail.com<br>Address: test<br>City: test<br>Country: null<br>Ad Category: test<br>Plan: null<br>Website: <br>Company name: test<br>Μήνυμα: test</er>
<br><br>
<button id="FailCopy" type="button"
onclick="copyToClipboard('er#error-details')">Copy div to clipboard</button>
答案 1 :(得分:6)
非jQuery解决方案,只需将包含换行符的字符串复制到剪贴板
为清晰起见,请参阅代码注释。
function copyStringWithNewLineToClipBoard(stringWithNewLines){
// Step 1: create a textarea element.
// It is capable of holding linebreaks (newlines) unlike "input" element
const myFluffyTextarea = document.createElement('textarea');
// Step 2: Store your string in innerHTML of myFluffyTextarea element
myFluffyTextarea.innerHTML = stringWithNewLines;
// Step3: find an id element within the body to append your myFluffyTextarea there temporarily
const parentElement = document.getElementById('any-id-within-any-body-element');
parentElement.appendChild(textarea);
// Step 4: Simulate selection of your text from myFluffyTextarea programmatically
myFluffyTextarea.select();
// Step 5: simulate copy command (ctrl+c)
// now your string with newlines should be copied to your clipboard
document.execCommand('copy');
// Step 6: Now you can get rid of your fluffy textarea element
parentElement.removeChild(myFluffyTextarea);
}
答案 2 :(得分:1)
您的代码可能运行良好,但是记事本无法处理Unix'\ n换行符,它只能处理\ r \ n,这就是您没有看到它们的原因。
请下载更高级的编辑器(如Notepad ++,https://notepad-plus-plus.org)并尝试将其粘贴到那里。不仅如此,它还有许多其他非常酷的功能,如语法高亮,宏和插件,所以值得将它用于更多目的。
如果您想让换行符在MS应用中运行,则需要在使用此行复制之前替换换行符:
$temp = $temp.replace(/\n/g, "\r\n");
要以HTML格式打印,您需要将\ n替换为\,如下所示:
$temp = $temp.replace(/\n/g, "<br>");
答案 3 :(得分:1)
有两件事是错的:
(1)根据text的jquery文档:
.text()方法的结果是一个包含所有匹配元素的组合文本的字符串。 (由于不同浏览器中HTML解析器的不同,返回的文本可能会在换行符和其他空格中有所不同。)
他们的例子,
<div class="demo-container">
<div class="demo-box">Demonstration Box</div>
<ul>
<li>list item 1</li>
<li>list <strong>item</strong> 2</li>
</ul>
</div>
代码$( "div.demo-container" ).text()
将产生:
Demonstration Box list item 1 list item 2
因此,只需使用html()
方法来获取innerHTML
。
(2)<input>
标记删除换行符。请改用textarea
。有关详细信息,请参阅:this answer。
希望这个spinet会起作用。
function copyToClipboard(element) {
var $temp = $("<textarea>");
$("body").append($temp);
var html = $(element).html();
html = html.replace(/<br>/g, "\n"); // or \r\n
console.log(html);
$temp.val(html).select();
document.execCommand("copy");
$temp.remove();
}
$( "#FailCopy" ).click(function() {
alert("Well done! div #error-details has been copy to your clipboard, now paste it in the notepad or email!");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!--text that i want to copy-->
<h2>Div #error-details: the text I want to copy to clipboard:</h2>
<er id="error-details">Name: test<br>Surname: test<br>Email: test@gmail.com<br>Address: test<br>City: test<br>Country: null<br>Ad Category: test<br>Plan: null<br>Website: <br>Company name: test<br>Μήνυμα: test</er>
<br><br>
<button id="FailCopy" type="button"
onclick="copyToClipboard('er#error-details')">Copy div to clipboard</button>
&#13;