如何在使用jQuery显示文本文件时保留换行符 [英] How to preserve newlines when showing a text file with jQuery
问题描述
我想获取文本文件的内容,我已经设法做到了。一切正常,除了一件事 - 检测新行。
I want to fetch the content of an text file and I have manage to do that. Everything is working except for one thing - detecting new lines.
我的解决方案来获取文本文件中的内容:
My solution to fetch the content in the text file:
$.ajax({
url: '/nhagyavi/files/textfiles/changelog.txt',
success: function(data) {
$('#load-changelog').html('<div style="font-family: Courier New;">' + data + '</div>');
}
});
文本文件的内容:
2012-03-15: Snabbfixar
- Detta är en fin liten rad som berättar vad som är nytt
- En till rad, tillsammans med en <a href="javascript:void(0)">länk</a>
我的网页上的结果如何:
How the result looks like on my page:
2012-03-15: Snabbfixar - Detta är en fin liten rad som berättar vad som är nytt - En till rad, tillsammans med en länk
我不知道如何在jQuery中使用 print_r
真的看看线条真的看起来像。
I don't know how I can use print_r
in jQuery so I can't really see how the lines really looks like. Anyone who knows how I can fix my problem?
推荐答案
添加 white-space:pre-line;
到您的容器:
#load-changelog {
white-space: pre-line;
}
简化演示: http://jsfiddle.net/tF3Mb/1/
如果您还想显示所有空格字符,请使用 white-space:pre
而不是 pre-line
: http://jsfiddle.net/tF3Mb/
If you also want to show all space characters, use white-space:pre
instead of pre-line
: http://jsfiddle.net/tF3Mb/
这篇关于如何在使用jQuery显示文本文件时保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!