jQuery将换行符转换为br(相当于nl2br) [英] jQuery convert line breaks to br (nl2br equivalent)
本文介绍了jQuery将换行符转换为br(相当于nl2br)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jQuery获取一些textarea内容并将其插入到li中。
I'm having jQuery take some textarea content and insert it into an li.
我希望它能直观地保留换行符。
I want it to visually retain the line breaks.
必须有一个非常简单的方法来做到这一点......
There must be a really simple way to do this...
推荐答案
演示: http://so.devilmaycode.it/jquery-convert-line-breaks-to-br-nl2br-equivalent
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
- http://phpjs.org/functions/nl2br:480
这篇关于jQuery将换行符转换为br(相当于nl2br)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文