admin管理员组文章数量:1394046
I have a <textarea>
element. When the user fills it, you can see the spaces they made and when they pressed Enter to jump to the next line.
This is great, but when I see the HTML output, the result differs. It is an endless sentence without line breaks.
Using only HTML or JavaScript, how can I fix this?
I have a <textarea>
element. When the user fills it, you can see the spaces they made and when they pressed Enter to jump to the next line.
This is great, but when I see the HTML output, the result differs. It is an endless sentence without line breaks.
Using only HTML or JavaScript, how can I fix this?
Share Improve this question edited Nov 5, 2012 at 8:32 Pekka 450k148 gold badges987 silver badges1.1k bronze badges asked Nov 5, 2012 at 8:28 user1799565user1799565 591 silver badge7 bronze badges 3- 1 Your question is very vague. Please post some code if you want to get a response – Mihai Commented Nov 5, 2012 at 8:30
- 1 Wele to SO. Please share your code or what you have tried to get good answers!!! – Rahul Tripathi Commented Nov 5, 2012 at 8:31
- 4 I don't think it's vague. The OP wants the line break behaviour in the textarea to match that of the HTML output. He doesn't want to use a full-blown WYSIWYG editor to fix the problem, and he's right. – Pekka Commented Nov 5, 2012 at 8:31
2 Answers
Reset to default 9You probably want something like:
<p style="white-space: pre-wrap"></p>
<p style="white-space: pre"></p>
<pre></pre>
pre-wrap:
Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks
pre:
Whitespace is preserved by the browser. Text will only wrap on line breaks Acts like the <pre> tag in HTML
If you are just taking what is entered in a textarea and outputing it as html, you would see this. For starters, you could replace all spaces with
and all newlines with <br>
. Or put the output in <pre>
tags. (In either case, you will also want to replace some other characters with entities too.)
版权声明:本文标题:javascript - How do I make the whitespace behaviour in a <textarea> and a HTML preview match? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744087251a2588753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论