【问题标题】:HTML - Newline char in DIV content editable?HTML - DIV 内容中的换行符可编辑?
【发布时间】:2013-10-03 00:02:45
【问题描述】:

我在数据库中存储内容,例如:

Hello
This
is 
Text

当我将它传递给文本区域时,它会保留新的换行符。但是,如果我将该文本传递给内容可编辑的 div,它将保持如下状态:

Hello This is Text

我该如何解决这个问题?

【问题讨论】:

标签: css html contenteditable


【解决方案1】:

在 div 上设置样式:white-space: prewhite-space: pre-wrap

示例:http://jsfiddle.net/fPv6S/

【讨论】:

  • 这个答案值得更多的赞许。如果有人需要white-space 的信息(我个人选择pre-wrap):developer.mozilla.org/en-US/docs/Web/CSS/white-space
  • 甚至white-space:pre-line
  • 这个问题是关于 contenteditable,而不是关于 textarea
  • 由于某种原因它在 android 上的 chrome 中不起作用?这是一个错误吗?我认为 chrome 创建 div 来创建新行 :(
  • 问题说 contenteditable,你的小提琴使用 textarea,它们完全不同 - 这个答案应该被忽略。
【解决方案2】:

您可以使用<br /> 搜索和替换换行符。 http://jsfiddle.net/fPv6S/1/

【讨论】:

    【解决方案3】:

    试试这个......

    var patt2 = new RegExp("<div>","g");
    var patt3= new RegExp("</div>","g");
    var patt4= new RegExp("<br>","g");
    var z=x.replace(patt2,"\n").replace(patt3,"").replace(patt4,"");
    

    这样就可以了……

    【讨论】:

      【解决方案4】:

      要在@Explosion Pills 正确答案上添加一些信息并从MDN 中提取一些信息:

      CSS white-space 属性可以提供帮助:

      前:

      white-space: pre
      

      保留空白序列。线只在 源和&lt;br&gt; 元素中的换行符。

      这可能会导致不需要的水平滚动条,如示例所示!

      预包装:

      white-space: pre-wrap
      

      保留空白序列。换行符换行 字符,&lt;br&gt;,并根据需要填充行框。

      正如@ceremcem 指出的那样,当文本被复制粘贴时,框末尾的换行符不会被转移,这是有道理的,因为这些换行符不是文本格式的一部分,而是视觉外观的一部分.

      预行:

      white-space: pre-line
      

      空白序列被折叠。换行符换行 字符,&lt;br&gt;,并根据需要填充行框。

      div{
        border: 1px solid #000;
          width:200px;
      }
      .pre {
          white-space: pre;
      }
      .pre-wrap{
         white-space: pre-wrap;
      }
      .pre-line{
         white-space: pre-line;
      }
      
      .inline-block{
          display:inline-block
      }
      <h2>
      pre
      </h2>
      <div class="pre" contenteditable=true>Lorem ipsum dolor sit amet doesn't have a meaning but here comes 10 white spaces:____          ____</div>
      
      <h2>
      pre-wrap
      </h2>
      <div class="pre-wrap" contenteditable=true>Lorem ipsum dolor sit amet doesn't have a meaning but here comes 10 white spaces:____          ____</div>
      
      <h2>
      pre-line
      </h2>
      <div class="pre-line"  contenteditable=true>Lorem ipsum dolor sit amet doesn't have a meaning but here comes 10 white spaces:____          ____</div>
      
      <h2>
      Chrome FIX
      </h2>
      <div class="pre-line inline-block"  contenteditable=true>Lorem ipsum dolor sit amet doesn't have a meaning but here comes 10 white spaces:____          ____</div>

      编辑 2018 年 8 月 14 日:

      在 Chrome 中您可能会遇到麻烦:按 Enter 将在您的 contenteditable 中生成一个新的&lt;div&gt;。为防止这种情况发生,您可以按 Shift+Enter 或将display: inline 设置为可编辑的&lt;div&gt;,如小提琴所示。

      【讨论】:

      • 你太棒了。非常感谢:)
      猜你喜欢
      • 1970-01-01
      • 2010-10-06
      • 2022-12-09
      • 2018-10-16
      • 2021-03-09
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多