Home >Web Front-end >JS Tutorial >How to use the JQuery trim() function
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)].
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!