我有一些这样的文字
unpivot
我使用此功能将其复制到剪贴板
text = 'line 1' + "\r\n";
text+= 'line 2' + "\r\n";
text+= 'line 3' + "\r\n";
如何在不丢失换行符的情况下将其复制到剪贴板?
答案 0 :(得分:0)
当我在函数中定义文本时,它似乎对我有用。您确定新行已按预期传递到函数中吗?
function copyToClipboard()
{
var text = 'line 1' + "\r\n";
text+= 'line 2' + "\r\n";
text+= 'line 3' + "\r\n";
var copyElement = document.createElement("span");
copyElement.appendChild(document.createTextNode(text));
copyElement.id = 'tempCopyToClipboard';
angular.element(document.body.append(copyElement));
// select the text
var range = document.createRange();
range.selectNode(copyElement);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
// copy & cleanup
document.execCommand('copy');
window.getSelection().removeAllRanges();
copyElement.remove();
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<button type="button" onclick="copyToClipboard()">Copy text to clipboard</button>
答案 1 :(得分:0)
对不起,我不好 我复制了此代码段,但我只是意识到我应该使用文本区域而不是跨度。