我想将文本复制到剪贴板中,但是在新行中。
问题:
如果您单击代码片段中的按钮并粘贴到记事本中,您将获得以下内容:
Name: testSurname: testEmail: test@gmail.comAddress: testCity: testCountry: nullAd Category: testPlan: nullWebsite: Company name: testΜήνυμα: test
我想要的:
如果可能,我希望在新行中复制文本。与复制时一样:
Name: test
Surname: test
Email: 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>
我也尝试使用以下CSS规则,将<br>
替换为\n
和\r\n
,并添加到我的div中:white-space:pre-wrap;
但是没有看到任何成功的迹象。