Html 在纯文本div中插入新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37811366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:24:08  来源:igfitidea点击:

Insert new line in text only div

htmlunicodeline-breaks

提问by tymothytym

I'm working with a CMS that allows only text in a certain div (HTML , like <br>, is parsed to &lt;br&gt;). It is fine with Unicode / HTML codes (e.g. &amp;would generate &and &#x00040;would generate @) but it will seemingly not allow a new line / carriage return. How can I put a <br>in the div, without using HTML?

我正在使用一个 CMS,它只允许某个 div 中的文本(HTML ,如<br>,被解析为&lt;br&gt;)。它适用于 Unicode/HTML 代码(例如&amp;会生成&&#x00040;会生成@),但它似乎不允许换行/回车。如何<br>在不使用 HTML 的情况下将 a放入 div?

I've tried:

我试过了:

Here is a fiddle with the same issue (and HTML structure as output by CMS) > https://jsfiddle.net/w3p4wgcc/...Is it just not possible?

这是一个具有相同问题的小提琴(以及 CMS 输出的 HTML 结构)> https://jsfiddle.net/w3p4wgcc/...这是不可能的吗?

回答by Baklap4

With inline css i've managed to do the following:

使用内联 css,我设法做到了以下几点:

<div style="white-space: pre-wrap;">
This is some new text,&#10;this text should be on a new line.
</div>

https://jsfiddle.net/6mqgrym9/

https://jsfiddle.net/6mqgrym9/

If you need all of your divs to be like this one could say in your stylesheet the following:

如果您需要所有的 div 都像这样,可以在样式表中说以下内容:

div {
    white-space: pre-wrap;
}

if these divs are contained in a root div (which has a id) one could do the following:

如果这些 div 包含在根 div(具有 id)中,则可以执行以下操作:

#idhere > div {
    white-space: pre-wrap;
}

If you're allowed to use some javascript you can try this:

如果你被允许使用一些 javascript 你可以试试这个:

<div>
    <script>
        document.currentScript.parentNode.innerHTML = 'This is some new text<br>this text should be on a new line.';
    </script>
</div>