text区域换行jquery

textarea line break jquery

本文关键字:jquery 换行 区域 text      更新时间:2023-09-26

我有一个"编辑描述"按钮,当我点击它时,文本消失并出现

        if (id == 'commedit') jQuery(this).html('<textarea>'+jQuery(this).text()+'</textarea>');
        else if (id == 'commsave') {
            jQuery(this).text(jQuery(this).find('textarea').val());
        }

在MySql中,我看到了这个文本-"tetee<br>afafaf<br>afafaf<br>afsafsasfasf",它会用换行符显示,但当我在Jquery附带的文本区域中单击"编辑"时,它会显示为没有换行符,当我单击保存时,它也会在我的描述字段中显示为一长行,没有换行符。所以我需要你的帮助

<br>是html分隔符。您希望'n出现在文本区域中。

jQuery(this).html().replace(/<br>/gi,"'n")

保存时,您希望将其改回breaks,并使用html()而不是text();

var elem = jQuery(this);
if (id === 'commedit') {
    var text = jQuery(this).html().replace(/<br>/gi,"'n");
    elem.html('<textarea>'+ text +'</textarea>');
} else if (id === 'commsave') {
    var html = elem.find('textarea').val().replace(/'n/g,"<br>");
    elem.html(html);
}

JSFiddle示例