【问题标题】:How to preserve white spaces in content editable div如何在内容可编辑的 div 中保留空格
【发布时间】:2013-10-28 23:57:45
【问题描述】:

我使用内容可编辑 div 作为字段来获取我网站中的用户输入。 但我的问题是,当我使用 jquery 获取 div 的内容时,用户在 div 中输入的所有空格都会丢失?

那么,我想知道有没有办法为内容可编辑的 div 保留所有空白?

谢谢

【问题讨论】:

  • white-space: pre 在使用 jquery 获取 div 的值时不起作用...我的浏览器是 chrome
  • white=spacewhite-space?
  • 您应该接受@iamio 的回答。它还修复了 Firefox 中的一个奇怪行为,即在可编辑 div 末尾键入的空格折叠。

标签: javascript jquery css html


【解决方案1】:

我在这里找到了答案:http://simonewebdesign.it/blog/how-to-make-browser-editor-with-html5-contenteditable/

内容可编辑元素的 css 样式应包含以下内容:

.content_editable_element {
    white-space: pre-wrap;       /* css-3 */
    white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
}

【讨论】:

  • 使用white-space: pre; 也可以防止文本换行
【解决方案2】:

div 有一个默认的空白:normal;。无论如何它是怎么消失的,你如何获取/获取内容?我觉得换行也会是个问题

<div class="input" contenteditable="true">abc 123 def 456</div>
<button class="get">get</button>   
$('.get').click(function(){alert($('.editable').text());});

JSFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多