jQuery-转换< br>和< br/>和< p/>等到新行 [英] jQuery - convert <br> and <br /> and <p /> and such to new line

查看:608
本文介绍了jQuery-转换< br>和< br/>和< p/>等到新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery-如何将<br><br /><p />等转换为新行?

jQuery - How do you convert <br> and <br /> and <p /> and such to new line?

jQuery是否具有内置的br2nl()函数-这用于将新行标签转换为用户友好的文本字段版本.

Does jQuery have a built in br2nl() function - this is for converting new lines tags to user-friendly textfield versions.

推荐答案

您可以创建如下函数:

jQuery.fn.nl2br = function(){
   return this.each(function(){
     jQuery(this).val().replace(/(<br>)|(<br \/>)|(<p>)|(<\/p>)/g, "\r\n");
   });
};

并像以下任何一种方式使用它:

And use it like any of these ways:

$(':input').nl2br();
$('textarea').nl2br();
$('#textarea_id').nl2br();

这篇关于jQuery-转换&lt; br&gt;和&lt; br/&gt;和&lt; p/&gt;等到新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆