The jQuery.trim() function is used to remove whitespace characters at both ends of a string. This function removes whitespace characters at the beginning and end of a string (until the first non-whitespace string is encountered). It will remove common whitespace characters including newlines, spaces, tabs, etc.
This function belongs to the global jQuery object.
Grammar
jQuery 1.0 added this static function.
jQuery.trim( str )
参数 | 描述 |
---|---|
str | String类型需要去除两端空白字符的字符串。 |
If the parameter str is not of string type, this function will automatically convert it to a string (usually calling its toString() method). If the parameter str is null or undefined, an empty string ("") is returned.
Return value
The return value of the jQuery.trim() function is of String type, returning the string after removing the blank strings at both ends.
Examples & Instructions
The following is jQuery sample code related to the jQuery.trim() function to demonstrate the specific usage of the jQuery.trim() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Usage of trim() in jquery
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
If you develop a website under the IE8 browser, this is actually a false proposition, because native javascript does not support the .trim() method. If you write code similar to document.getElementByID().trim(); When running in IE8 mode, an error will be reported: The method or attribute is not supported.
The solution is as follows:
1. Use $.trim([object to be operated on]);
provided by JQuery2. Encapsulate a method to intercept blank characters.