Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

css - 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>');
    }
});

The content of the text file:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add white-space:pre-line; to your container:

#load-changelog {
    white-space: pre-line;
}

Simplified demo: http://jsfiddle.net/tF3Mb/1/

If you also want to show all space characters, use white-space:pre instead of pre-line: http://jsfiddle.net/tF3Mb/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...