JQuery. trim () function and trim () Usage Details, jquery. trimtrim
The jQuery. trim () function removes spaces at both ends of a string. This function can remove the white spaces at the start and end of the string (until the first non-white string is encountered ). It clears common blank characters including line breaks, spaces, and tabs.
This function is a global jQuery object.
Syntax
JQuery 1.0 adds this static function.
JQuery. trim (str)
Parameters |
Description |
Str |
The String type must remove the white spaces at both ends. |
If the str parameter is not of the string type, this function automatically converts it to a string (generally calls its toString () method ). If the str parameter is null or undefined, an empty string ("") is returned ("").
Return Value
The Return Value of the jQuery. trim () function is of the String type. The return value is the String after the blank strings at both ends are removed.
Example & Description
The following is the jQuery Sample Code related to the jQuery. trim () function to demonstrate the specific usage of the jQuery. trim () function:
// Append the line feed label and the specified HTML content to the current page. function w (html) {document. body. innerHTML + = "<br/>" + html;} // double quotation marks are added to both sides of the output to distinguish string boundary w ('"' + $. trim ("CodePlayer") + '"'); //" CodePlayer "// only the consecutive white space characters w ('"' + $. trim ("Code Player") + '"'); //" Code Player "w ('"' + $. trim ("\ r \ n \ t CodePlayer \ t") + '"'); //" CodePlayer "w ('"' + $. trim ("") + '"'); //" w ('"' + $. trim (12) + '"'); //" 12 "w ('"' + $. trim (null) + '"'); //" w ('"' + $. trim (undefined) + '"'); //" w ('"' + $. trim (new Object () + '"'); //" [object Object]"
Trim () usage in jquery
If you develop a website in the IE 8 browser, this is actually a false proposition, because native javascript does not support it. trim () method. If you write something similar to document. getElementByID (). trim (); Code, run in IE8 mode, will report: does not support this method or attribute error.
The solution is as follows:
1. Use $. trim ([object to be operated]) provided by JQuery.
2. encapsulate a method to intercept blank characters.