如何在内容可编辑div中保留空格

Vin*_*rma 18 html javascript css jquery html5

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

所以,我想知道有没有办法保留内容可编辑div的所有空格?

谢谢

iam*_*mio 29

我在这里找到答案: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+ */
}
Run Code Online (Sandbox Code Playgroud)

  • 我可以确认这是有效的并且挽救了我的生命! (2认同)

Mar*_*k S 1

div 有一个默认的空白:normal;。它是怎么消失的以及如何获取/获取内容?我认为换行符也会是一个问题

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

JSFiddle