What is the use of jquery trim() method

青灯夜游
Release: 2022-03-10 17:57:31
Original
1947 people have browsed it

In jquery, the trim() method is used to remove whitespace characters at both ends of the string. The syntax is "$.trim(str)"; the trim() method will remove all whitespace characters at the beginning and end of the string. Newlines, spaces (including consecutive spaces), and tabs.

What is the use of jquery trim() method

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery trim() method

The trim() method is used to remove whitespace characters at both ends of the string.

Syntax:

$.trim( str )
Copy after login

str parameter: It is a String type and specifies a string that needs to remove whitespace characters at both ends.

Note: $.trim() 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.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
	</head>
	<body>

		<pre id="original">
Copy after login
Copy after login
<script> $(function() { var str = " lots of spaces before and after "; $("#original").html("Original String: &#39;" + str + "&#39;"); $("#trimmed").html("$.trim()&#39;ed: &#39;" + $.trim(str) + "&#39;"); }) </script>

What is the use of jquery trim() method

##Note: The $.trim() function will work in jQuery 3.5 and above The version is obsolete and can be replaced by JavaScript native String.prototype.trim.

【Recommended learning:

jQuery video tutorial, web front-end video

The above is the detailed content of What is the use of jquery trim() method. 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