替换换行符以使它们使用jquery [英] replace linebreaks to get them working with jquery

查看:53
本文介绍了替换换行符以使它们使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提交了一些小脚本来预览一些文本,然后再将其提交到数据库中存储:

I have following small script to preview some text before submitting it to store in a database:

jQuery(function($) {
    var input = $('#contents'),
    preview = $('#previewaccordion div.viewcontents');

    input.keyup(function(e) {
        preview.html(input.val());
    });
});

但是如果我用换行符键入文本,它会忽略它们并将所有这些写入一行。我怎么能更换换行符以便它们正确显示?

but if I type text with line-breaks it ignores them and writes all of them in one line. How could I replace the line-breaks so that they show correctly?

推荐答案

我假设您使用textarea输入。有\ n用作换行符,它们对HTML没有影响。所以你必须用br-tags替换它们:

I assume that you are using a textarea for the input. There are \n used as linebreaks, which have no influence in HTML. So you have to replace them with br-tags:

input.val().replace(/\n/g, "<br />");

这篇关于替换换行符以使它们使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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