JQuery. trim () function and trim () Usage Details, jquery. trimtrim

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.