To delete string spaces in jquery, you can use $. trim () to remove string spaces. Next I will introduce you to anyone who needs to know about it.
It is easy to use JQuery to delete spaces before and after a string. use $. trim (StringText); // StringText is the string to delete spaces.
$. Trim (str)
Return Value: string;
Note: Remove spaces at the beginning and end of the string.
Example:
First look at an error code:
The Code is as follows: |
Copy code |
<Input type = "text" name = "" id = "results" value = ""/> Var content = $ ('# content'). val (); If (content. trim () = '') Alert ('null '); |
No error is reported in firefox, but an error is reported in ie.
Therefore, you must write the following format:
The Code is as follows: |
Copy code |
<Input type = "text" name = "" id = "results" value = ""/> Var content = $ ('# results'). val (); If ($. trim (content) = '') Alert ('null '); Or Var content = $ ('# content'). val (); If (jQuery. trim (content) = '') Alert ('null '); |
One instance
The Code is as follows: |
Copy code |
<! Doctype html> <Html> <Head> <Script src = "http://code.jquery.com/jquery-1.5.js"> </script> </Head> <Body> <Button> show trim example </button> <Script> $ ("Button"). click (function (){ Var str = "lots of spaces before and after "; Alert ("'" + str + "'"); Str = jquery. trim (str ); Alert ("'" + str + "'-no longer "); }); </Script> </Body> </Html> |