admin管理员组文章数量:1221786
Is it possible to extract the plaintext from a contenteditable div, including newlines? The jQuery $.text() method strips out newlines, which I need. The solution can use jQuery.
Is it possible to extract the plaintext from a contenteditable div, including newlines? The jQuery $.text() method strips out newlines, which I need. The solution can use jQuery.
Share Improve this question asked Jul 29, 2011 at 2:52 penguinrobpenguinrob 1,4593 gold badges17 silver badges39 bronze badges 3 |3 Answers
Reset to default 9Without using extra plugins or writing your own implementation, you can just use both the innerText and textContent attributes (which are equivalent to each other). textContent is supported in all major browsers except IE 6-8, which supports innerText.
var text = x.innerText || x.textContent
http://www.quirksmode.org/dom/w3c_html.html#t07
EDIT: as AgentME points out, this won't preserve the user's whitespace in Firefox. To do that with contenteditable, you'd have to polyfill innerText on Firefox. There are a couple of options I could come up with:
- Start with
x.innerHTML
, strip out the extra markup whitespace/tags and convert the user's whitespace from<br>
and
to\n
and spaces. - Use Firefox's
Selection
andRange
support. Firefox supportsSelection.toString
which has a similar behavior toinnerText
, but it only works on the currently-selected text. So what you can do is record the user's current selection, change the selection to be the contents of your contenteditable div, callSelection.toString
, then restore the user's initial selection.
Out of the two I personally think the second option would be better in most cases; with the first option you'll either get a quick-and-dirty regex solution or you devolve into implementing a full-blown HTML parser with CSS layout logic. The downside to option 2 is that it's relatively slow, so you may run into issues if you trigger it onchange
or something like that. Demo of option 2 is here.
More info:
- Mozilla API docs for Selection
- Speed test of innerText vs Selection.toString
With a bit of tweaking, https://github.com/vorushin/jsHtmlToText was just what I needed.
There's no easy, cross-browser way. innerText
does what you want in some (but not all) browsers. Setting the selection to encompass the editable element and calling toString()
does what you want in a different collection of browsers. In short, there's no easy way: you need to traverse the DOM and add line breaks in as appropriate for <br>
and block-level elements. I certainly wouldn't recommend using any regex-based solution, such as the one you seem to have settled on, because it can never work for all possible HTML.
Self-promotion: I will be adding this to my Rangy library in the reasonably near future.
本文标签: javascriptGet plain text from contenteditable divStack Overflow
版权声明:本文标题:javascript - Get plain text from contenteditable div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739348744a2159279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<pre>
or searching for answers that mention pre-formatted text will help you find a solution. – Wex Commented Jul 29, 2011 at 2:57