Jquery textarea val() doesnt take new line

给你一囗甜甜゛ 提交于 2019-12-23 18:00:21

问题


I get a textarea with a keyup function attached. When the user type something, I want this text to be rendered inside the blockquote tag. It works, but id doesn't take the newline and spaces. Can you helpe me?

$('#post_body').keyup(function() {    
$('blockquote').find('span').text($(this).val());

回答1:


It does take a newline, however those are usually ignored when rendering HTML. However, you can give that <span> (or the <blockquote>) newline rendering to match by setting it's white-sapce to be the same as a <pre> element, like this:

blockquote span { white-space: pre; }​
//or..
blockquote { white-space: pre; }​

You can test it out here.




回答2:


A raw solution would be to replace all newlines with <br/>; tag and all spaces with &amp;nbsp;



来源:https://stackoverflow.com/questions/3948779/jquery-textarea-val-doesnt-take-new-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!