热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

contentEditable字段,用于在数据库输入时维护换行符-contentEditablefieldtomaintainnewlinesupondatabaseentry

IhaveadivwiththeattributecontentEditableset.我有一个div属性contentEditable设置。Thisallowsmeto

I have a div with the attribute contentEditable set.

我有一个div属性contentEditable设置。

This allows me to have a free-form editable textarea, without the need for any form input fields: http://jsfiddle.net/M8wx9/8/

这允许我有一个自由格式的可编辑textarea,而不需要任何表单输入字段:http://jsfiddle.net/M8wx9/8/

However, when I create a couple of new lines and click save button, I grab the content with the .text() method which goes on to remove the line feeds I just entered. I need to maintain the newline characters if at all possible and store the content as plain text in the database.

但是,当我创建几个新行并单击“保存”按钮时,我使用.text()方法获取内容,该方法继续删除我刚刚输入的换行符。如果可能的话,我需要维护换行符,并将内容作为纯文本存储在数据库中。

I could store the HTML directly to the database by using .html() instead of .text() but I don't like that idea as I may need to extract this data as plain text in the future. Furthermore, pressing enter in Firefox breaks new lines with
, Chrome and Safari are breaking with

...
and Internet Explorer and Opera are using paragraphs

...

for new lines, so it doesn't sound very easy to be able to parse the html.

我可以使用.html()而不是.text()将HTML直接存储到数据库中,但我不喜欢这个想法,因为我可能需要将此数据提取为未来的纯文本。此外,在Firefox中按Enter键会打破新的行,Chrome和Safari正在打破

... ,Internet Explorer和Opera正在使用段落

... 线条,所以听起来很难解析html。

How can I preserve these line feeds and store the content as plain text in the database (Similar to the way a textarea does)?

如何保留这些换行符并将内容作为纯文本存储在数据库中(类似于textarea的方式)?

Best Regards, ns

最诚挚的问候,ns

3 个解决方案

#1


23  

jQuery uses textContent on a Node to return the text value of an element, which will compress white space. If you want the line breaks to be maintained, you need to use innerText which means accessing the element directly rather than through jQuery.

jQuery使用Node上的textContent来返回元素的文本值,这将压缩空白区域。如果你想要维护换行符,你需要使用innerText,这意味着直接访问元素而不是通过jQuery。

From your jsfiddle:

从你的jsfiddle:

console.log($(Comments)[0].innerText);

http://jsfiddle.net/M8wx9/10/

==== update:
As a caveat to this (as Tim Down has pointed out), using innerText will only work in webkit and microsoft browsers. If your application also supports Firefox, you will need to use regular expressions on the innerHTML in order to maintain line breaks. As an example:

====更新:作为对此的一个警告(正如Tim Down指出的那样),使用innerText只能在webkit和microsoft浏览器中使用。如果您的应用程序也支持Firefox,则需要在innerHTML上使用正则表达式以保持换行符。举个例子:

$(Comments).html().trim()
   .replace(//ig, '\n') // replace single line-breaks
   .replace(/<[p|div]\s/ig, '\n$0') // add a line break before all div and p tags
   .replace(/(<([^>]+)>)/ig, "");   // remove any remaining tags

http://jsfiddle.net/M8wx9/12/

#2


1  

function readContentWithBreaks(elem){
  if(elem[0].innerText){
    return elem[0].innerText.replace(/\n/ig,"
"); }else{ return elem.html(); } }

Since webkit browsers support innerText I decided to detect for that swap the /n they add. For Firefox it already gives the content with
so we can just take it as-is.

由于webkit浏览器支持innerText,我决定检测它们添加的/ n。对于Firefox,它已经为内容提供了
所以我们可以按原样使用它。

#3


-1  

This sounds too simple, so I am hesitant in giving this answer, but from a quick test I ran, the following should work perfectly all right (in chrome at least):

这听起来太简单了,所以我对这个答案犹豫不决,但是从我跑的快速测试来看,以下应该可以完美地工作(至少在chrome中):

$(".post-text").text().replace(/\n/g,"
")

You do have to start with a separate p or div tag though, so a text node followed by a paragraph element will not generate a line feed character.

您必须从单独的p或div标记开始,因此文本节点后跟段落元素将不会生成换行符。

And a jsfiddle shoing this. Still, as you already mention contentEditable isn't extremely cross browser consistent... so it might be a good idea to use an existing editor really.

并且有一个jsfiddle嘲笑这个。尽管如此,正如您已经提到的,contentEditable并不是非常跨浏览器的一致性......因此,真正使用现有编辑器可能是个好主意。


推荐阅读
author-avatar
捕鱼达人qq怎么刷金币
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有