简体   繁体   English

获取内容可编辑的元素值

[英]Get contenteditable element value

How do I get the value of a contenteditable element? 如何获得contenteditable元素的值?

Right now I just use innerHTML to get the content of the element, which would result carriage returns as <br> tags, so I need to convert <br> tags to \\n right now. 现在,我仅使用innerHTML来获取元素的内容,这将导致回车符作为<br>标记返回,因此我现在需要将<br>标记转换为\\n

Is there another more proper way to get the value from a contenteditable element that I just don't know about? 还有另外一种更合适的方法来从我不知道的contenteditable元素中获取价值吗?

Update: 更新:

In a textarea element when you get the value eg textarea.value the content is intact, like the carriage returns. textarea元素中,当您获取值(例如textarea.value ,内容是完整的,就像回车一样。 Is there a similar way to get value from a contenteditable element or I am forced to replace values? 是否有类似的方法可从contenteditable元素获取价值,或者我被迫替换价值?

content.innerHTML.replace(/<br\s*[\/]?>/gi, "\n")

element.textContent only gets the text, carriage returns not included. element.textContent仅获取文本,不包含回车符。 So this does not solve my problem. 因此,这不能解决我的问题。

If you're looking to let users edit text the only clean solution is to go with a text area as you figured out yourself. 如果要让用户编辑文本,唯一的解决方法是根据自己的想法选择文本区域。

If for some reason you can't or you don't wanna use a textarea you have write your own bit of code that will convert those <br> into carriage returns. 如果由于某种原因您不能或不想使用文本区域,那么您已经编写了自己的代码,将这些<br>转换为回车符。 There is no automatic way of doing this because there could be all sorts of markup in there. 没有自动执行此操作的方法,因为其中可能存在各种标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM