如何使用jQuery获取textarea的文本并保留换行符? [英] how to get textarea's text using jQuery with newline character preserved?

查看:770
本文介绍了如何使用jQuery获取textarea的文本并保留换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入文字的文字区域。我想检索用户输入的文本,包括自动换行的新行字符。如何使用保留换行符的jQuery来获取textarea的文本?

I have a text area in which user enters text. I want to retrieve the text as user entered including the new line character which is automatically wrapped. how to get textarea's text using jQuery with newline character preserved?

推荐答案

鉴于你的编辑,它会很难,如果没有不可能准确地做到这一点。问题是自动换行实际上并没有插入任何换行符,只是浏览器将一个字符串渲染成多行...所以我能想到的唯一方法是尝试&根据 textarea 的宽度编写一些代码来估计换行符的呈现位置并手动插入它们。

Given your edit, its going to be pretty hard, if not impossible to do it accurately. The problem is that automatic wrapping doesn't actually insert any newline characters, it's just the browser rendering the one string across multiple lines... So the only way I can think of is to try & write some code to estimate where the newlines were rendered based on the width of the textarea and manually insert them.

因此,记住如果您的文字区域没有CSS宽度设置,您使用 cols =xx属性你使用固定宽度字体你总是有滚动条显示(这会影响textarea根据 cols呈现的宽度),然后你应该能够计算单行上显示的单词数,然后通过这样的方式自动包装在浏览器中:

So with that in mind if your text area doesn't have a CSS width set and you use the cols="xx" attribute and you use a fixed width font and you always have the scrollbars showing (this effects how wide the textarea is rendered based of cols), then you should be able calculate the number of words displayed on a single line before it gets automatically wrapped by the browser by doing something like this:

//Based off <textarea id="text" cols="50" rows="5" style="overflow: scroll">This is a test sentence, with no newline characters in it, but with an automatically wrapped sentence that spans three lines.</textarea>
var words = $("#text").val().split(" ");
var cols = $("#text").attr("cols");
var text = "";
var lineLength = 0;
for (var i = 0; i < words.length; i++) {
    var word = words[i];
    if ((lineLength + word.length + 1) > cols) {
        text += "\n";
        lineLength = 0;
    } else if (lineLength > 0) {
        text += " ";
        lineLength++;
    }
    text += word;
    lineLength += word.length;
}
alert(text);
//Alerts:
//This is a test sentence, with no newline
//characters in it, but with an automatically
//wrapped sentence that spans three lines.

然而,这是如果的很多,而我不是知道这对所有浏览器的效果如何(我测试了它并且它似乎适用于Firefox 3.6)...

However, that's a lot of ifs, and I don't know how well this would work across all browsers (I tested it & it appears to work in Firefox 3.6)...

还要注意你是否沿着这条路走下去,我的自动换行代码非常简陋,几乎保证不会与浏览器在所有情况下所做的事情相匹配,因此您可能需要根据您的具体要求进行扩展。例如,长于 cols 长度的单词会被firefox拆分,但不会被我的代码拆分。

Also note if you do go down this route, that my word wrapping code is very rudimentary and is virtually guaranteed not to match what the browser is doing in all cases, so you may need to expand on it depending on what your exact requirements are. For example, words longer than the cols length get split by firefox, but not by my code.

这篇关于如何使用jQuery获取textarea的文本并保留换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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