简体   繁体   中英

How to insert HTML ASCII art from document?

At the moment i'm trying to read .txt files which could contain ASCII arts and display them in my HTML document with JQuery. Simple text is displayed right but i have formatting problems with the arts.

在此输入图像描述 在此输入图像描述

How can i fix this ?

$(document).ready(function(e) {

    $.get('/test/posts/header.txt', function(data) {

    var lines = data.split('\n');

    for (var i = 0, len = lines.length; i < len; i++) {

    $(".stream").append('<div class="line"><p class="header">' + lines[i] + '</p></div>');
    }

  });
});

This is because the excess spaces are getting stripped. Either you replace all the spaces with &nbsp in every line with simple ... + lines[i].replace(/ /g,'&nbsp') + ... , or you wrap everything in <pre>...</pre> :

'<div class="line"><p class="header"><pre>' + lines[i] + '</pre></p></div>'

HTML

<pre id="asciiart"></pre>

I see you are using jquery:

$("#asciiart" ).load( "/test/posts/header.txt" );

You can also use css

.line{
    white-space: nowrap;
}

or in your case

<div class="line"><p class="header"></div></div>

.line>.header

I think that's right.

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM