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?

Share Improve this question edited Jan 1, 2017 at 16:23 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Oct 16, 2013 at 10:15 Sujith WayanadSujith Wayanad 5436 silver badges21 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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