简体   繁体   English

HTML导入的文本不能正确显示换行符

[英]HTML Imported text doesn't show line feeds correctly

I have an HTML file which has an included text file. 我有一个HTML文件,其中包含一个文本文件。 This is my javascript for including text file: 这是我的JavaScript,用于包含文本文件:

document.include = function (url) {
    if ('undefined' == typeof(url)) return false;
        var p,rnd;
        if (document.all){
        p = new ActiveXObject("Microsoft.XMLHTTP");
        }
    else {
        p = new XMLHttpRequest();
    }
    rnd = Math.random().toString().substring(2);
    url = url.indexOf('?')>-1 ? url+'&rnd='+rnd : url+'?rnd='+rnd;
    p.open("GET",url,false);
    p.send(null);
    document.write( p.responseText );
};

This is my HTML: 这是我的HTML:

<html>
    <head>
        <link media="all" href="style.css" type="text/css" rel="stylesheet">
        <title>Report</title>
        <script src="embed_text.js" type="text/javascript"></script>
    </head>
    <body>
        <script>document.include('out.log');</script>
    </body>
</html>

Unfortunately imported content does not have any line feed! 不幸的是,导入的内容没有任何换行! All lines are appended to each other; 所有行都相互附加; but when I go to inspect mode, all lines are showing correctly! 但是当我进入检查模式时,所有行都正确显示! I mean content is showing in separate lines. 我的意思是内容显示在单独的行中。 What should I do to solve the problem. 我应该怎么做才能解决问题。 Solutions including CSS or javascript are completely fine! 包括CSS或javascript在内的解决方案都可以!

This is how content differs in HTML view and inspect mode: 这是内容在HTML查看和检查模式下的不同之处:

在此处输入图片说明

That's because your code is being parsed as HTML, and thus new lines and spaces beyond the first are being removed. 那是因为您的代码被解析为HTML,因此,第一行之外的新行和空格都将被删除。

Try wrapping your text within <pre> tags, and you should see the desired output. 尝试将文本包装在<pre>标记中,您将看到所需的输出。

You can try to add this code to your html: 您可以尝试将此代码添加到html中:

<style>
    body {
        white-space: pre-line;
    }
</style>

More about white spacing: http://www.w3schools.com/cssref/pr_text_white-space.asp 有关白色间距的更多信息: http : //www.w3schools.com/cssref/pr_text_white-space.asp

If there are other components in your html body, not just text from log, then wrap it in a div and change rule selector. 如果您的html主体中还有其他组件,而不仅仅是日志中的文本,则将其包装在div中并更改规则选择器。

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

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