Home  >  Article  >  Web Front-end  >  How to use the JQuery trim() function

How to use the JQuery trim() function

coldplay.xixi
coldplay.xixiOriginal
2020-11-19 11:53:342509browse

How to use the JQuery trim() function: [$.trim()] The function will remove all newlines, spaces and tabs at the beginning and end of the string. If these whitespace characters are in the string In the middle, it will not be removed, and the syntax is [$.trim(str)].

How to use the JQuery trim() function

The operating environment of this tutorial: Windows 7 system, jquery version 3.3.1. This method is suitable for all brands of computers.

How to use the JQuery trim() function:

Definition and usage

$.trim() Function is used to remove whitespace characters at both ends of a string. The

$.trim() function will be deprecated in jQuery 3.5 and above, and can be replaced by the JavaScript native String.prototype.trim.

Note: The $.trim() function will remove all newline characters, spaces (including consecutive spaces) and tab characters at the beginning and end of the string. If these whitespace characters are in the middle of the string, they are retained and not removed.

Syntax

$.trim( str )

Example

Remove spaces at the beginning and end of the string

$(function () { 
    var str = "         lots of spaces before and after         ";
    $( "#original" ).html( "Original String: '" + str + "'" );
    $( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
})

Related free learning recommendations: JavaScript (video)

The above is the detailed content of How to use the JQuery trim() function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn