只在文本中插入新行 [英] Insert new line in text only div

查看:155
本文介绍了只在文本中插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMS,它只允许特定div中的文本(HTML,比如< br> ,被解析为& amp ; LT峰; br&安培; GT; )。使用Unicode / HTML代码(例如& amp; )会生成& &#x00040; 会生成 @ ),但它看起来不允许换行/回车。我怎样才能在div中添加一个< br> 而不使用HTML?



我试过了:



这里有一个问题(和CMS输出的HTML结构相同)> https://jsfiddle.net/w3p4wgcc/ ...难道不可能?

解决方案

使用内联css我已经设法做到以下几点:

 < div style =white-space:pre-wrap;> 
这是一些新的文本,&#10;这个文本应该在一个新的行。
< / div>

https://jsfiddle.net/6mqgrym9/



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

  div {
white-space:pre-wrap;





$ b

如果这些div包含在一个根div(它有一个id)一个可以做到以下几点:

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



$ b $ p
$ b

如果你可以使用一些javascript,你可以试试这个: p>

 < div> 
< script>
document.currentScript.parentNode.innerHTML ='这是一些新的文本< br>这段文字应该放在新的一行。
< / script>
< / div>


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?

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?

解决方案

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

<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/

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

div {
    white-space: pre-wrap;
}

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

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

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

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

这篇关于只在文本中插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆