开发者

Jquery textarea val() doesnt take new line

开发者 https://www.devze.com 2023-01-20 14:24 出处:网络
I get a textarea with a keyup开发者_Python百科 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 an

I get a textarea with a keyup开发者_Python百科 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());


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消