admin管理员组文章数量:1410682
Here is my question,
I have a text file and I am reading the file using jquery.
the code is here
$(function() {
$.get('files/james.txt', function(data) {
$('.disease-details').html(data);
}, 'text');
});
but I am getting only plain text and all formatting is disappearing.
I want to convert all the enter in to p
tag. is it possible?
Here is my question,
I have a text file and I am reading the file using jquery.
the code is here
$(function() {
$.get('files/james.txt', function(data) {
$('.disease-details').html(data);
}, 'text');
});
but I am getting only plain text and all formatting is disappearing.
I want to convert all the enter in to p
tag. is it possible?
- I would like to see the content of this text file ... – Stphane Commented Oct 16, 2013 at 10:17
- something like this? stackoverflow./questions/1155678/… – Tim Vermaelen Commented Oct 16, 2013 at 10:18
- You could use a regular expression. – Flea777 Commented Oct 16, 2013 at 10:19
- 1 my text is not html text. I have only text with 3 paragraph it is separated with only enter – Sujith Wayanad Commented Oct 16, 2013 at 10:25
3 Answers
Reset to default 5You can do this :
$(function() {
$.get('files/james.txt', function(data) {
data = data.split("\n");
var html;
for (var i=0;i<data.length;i++) {
html+='<p>'+data[i]+'</p>';
}
$('.disease-details').html(html);
}, 'text');
});
The above splits up the text by new line (the text-"formatting") and wraps each chunk into a <p>
.. </p>
.. Exactly as requested.
By definition, a plain-text file has no formatting to begin with (hence the "plain" part).
If you mean newline characters, those are not rendered in HTML by default. To overe this, you can simply wrap your text with a <pre>
tag which among other things is rendered including newline characters.
For example:
$(function() {
$.get('files/james.txt', function(data) {
$('.disease-details').html($('<pre>').text(data)); // Text wrapped in <pre>
}, 'text');
});
You will either have to get the file pre-formatted using HTML tags / or just get the data from the text file and do a client side formatting ( using templates or otherwise ). Can give you better pointers if you give sample of what the data in the file would look like
本文标签: javascriptFormatting text file using jqueryStack Overflow
版权声明:本文标题:javascript - Formatting text file using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744954996a2634303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论