问题
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 &nbsp;
来源:https://stackoverflow.com/questions/3948779/jquery-textarea-val-doesnt-take-new-line