Home > Web Front-end > JS Tutorial > body text

How to use the JQuery trim() function

coldplay.xixi
Release: 2020-11-30 16:55:27
Original
2572 people have browsed it

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 )
Copy after login

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) + "'" );
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template