jQuery 使用jQuery从textarea中修剪前导/尾随空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3721999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:58:49  来源:igfitidea点击:

Trim leading/trailing whitespace from textarea using jQuery?

jquerywhitespacetrimtrailing

提问by caroline

Following code is an example of text placed into a textarea from a database.

以下代码是将文本从数据库放入 textarea 的示例。

<textarea id="inputPane" cols="80" rows="40" class="pane">
<p>
    some text here...
</p>
  <p>
    more text here...
</p>
</textarea>

using jQuery's .trim what is the actual jquery code to remove all leading and trailing whitespace and have the textarea display very similar to below?

使用 jQuery 的 .trim 删除所有前导和尾随空格并使 textarea 显示与下面非常相似的实际 jquery 代码是什么?

<textarea id="inputPane" cols="80" rows="40" class="pane">
    <p>some text here...</p>
    <p>more text here...</p>
</textarea>

I've worked on this for hours with no success trying varying combinations with .trim

我已经为此工作了几个小时,但没有成功尝试与 .trim 的不同组合

$('#inputPane')jQuery.trim(string);

采纳答案by Kevin

You could try something like this:

你可以尝试这样的事情:

jQuery(function(?$) {
    var pane = $('#inputPane');
    pane.val($.trim(pane.val()).replace(/\s*[\r\n]+\s*/g, '\n')
                               .replace(/(<[^\/][^>]*>)\s*/g, '')
                               .replace(/\s*(<\/[^>]+>)/g, ''));
});?

Which gives the result:

这给出了结果:

<p>some text here...</p>
<p>more text here...</p>

Though this may not be bulletproof, it should prove to be much faster/more efficient than creating elements from the HTML value of the textarea.

虽然这可能不是万无一失的,但它应该被证明比从 textarea 的 HTML 值创建元素更快/更有效。

回答by user113716

Try this:

尝试这个:

var $input = $('#inputPane');

var $container = $('<div>').html( $input.val() );

$('*', $container).text( function(i,txt) {
    return $.trim( txt );
});

$input.val( $container.html() );

It turns the content of the textareainto elements, walks through them and trims the content, then inserts the resulting HTML back into the textarea.

它将 的内容textarea转换为元素,遍历它们并修剪内容,然后将生成的 HTML 插入回textarea.



EDIT:Modified to use .val()instead of .text()as noted by @bobince

编辑:修改为使用.val()而不是.text()@bobince 指出的

回答by Mottie

This is how I would do it (demo):

这就是我的做法(演示):

$('.pane').val(function(i,v){
    return v.replace(/\s+/g,' ').replace(/>(\s)</g,'>\n<');
});

回答by Vlad

You don't need jQuery to Trim leading/trailing whitespace from textarea. You need to code it in 1 line

您不需要 jQuery 来修剪 textarea 中的前导/尾随空格。您需要在 1 行中对其进行编码

Before:

前:

<textarea id="inputPane" cols="80" rows="40" class="pane">
    <p>some text here...</p>
    <p>more text here...</p>
</textarea>

After:

后:

<textarea id="inputPane" cols="80" rows="40" class="pane"><p>some text here...</p></textarea>

回答by DMI

jQuery.trim()will remove leading and trailing whitespace from the entire string -- in this case, before the first <p>and after the last </p>. You want something more complex, which is to remove whitespace between certain tags. This is not necessarily easy, but could (perhaps!) be accomplished with a regular expression, for example:

jQuery.trim()将从整个字符串中删除前导和尾随空格 - 在这种情况下,在第<p>一个</p>. 您想要更复杂的东西,即删除某些标签之间的空格。这不一定容易,但可以(也许!)用正则表达式完成,例如:

// assuming val is the textarea contents:
val = val.replace(/>\s*</, '><').replace(/\s+$/, '');

DISCLAIMER:This was just quickly put together and may not cover every case.

免责声明:这只是快速组合在一起,可能无法涵盖所有​​情况。

回答by kevingessner

Get the value, trim the value, set the value:

获取值,修剪值,设置值:

var value = $('#inputPane').val();
value = $.trim(value);
$('#inputPane').val(value);

Or in one line:

或者在一行中:

$('#inputPane').val($.trim($('#inputPane').val()));