【发布时间】:2018-02-12 23:22:26
【问题描述】:
我想将文本复制到剪贴板,但要换行。
问题:
如果您单击 sn-p 中的按钮并粘贴到记事本中,您将得到以下内容:
姓名:testSurname:testEmail:test@gmail.com地址:testCity:testCountry:null广告类别: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>
我还尝试通过在我的 div 中添加以下 css 规则:white-space:pre-wrap;,将 <br> 替换为 \n 和 \r\n,但没有任何成功迹象。
【问题讨论】:
标签: javascript jquery copy clipboard