简体   繁体   English

如何从文档中插入HTML ASCII艺术?

[英]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. 目前我正在尝试读取可能包含ASCII艺术的.txt文件,并使用JQuery在我的HTML文档中显示它们。 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> : 或者您更换所有的空格&nbsp用简单的在每一行... + lines[i].replace(/ /g,'&nbsp') + ... ,或者包裹在一切<pre>...</pre>

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

HTML HTML

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

I see you are using jquery: 我看到你正在使用jquery:

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

You can also use css 你也可以使用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 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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