jQuery.trim()

来源:互联网 发布:食品安全专业出国 知乎 编辑:程序博客网 时间:2024/05/18 01:49

jQuery.trim( str )Returns: String

Description: Remove the whitespace from the beginning and end of a string.

  • version added: 1.0jQuery.trim( str )

    strThe string to trim.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

Examples:

Example: Remove the two white spaces at the start and at the end of the string.

<!DOCTYPE html> <html> <head>   <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body>            <pre id="original"></pre>         <pre id="trimmed"></pre>        <script>   var str = "         lots of spaces before and after         ";   $("#original").html("Original String: '" + str + "'");   $("#trimmed").html("$.trim()'ed: '" + $.trim(str) + "'"); </script>  </body> </html>

Example: Remove the two white spaces at the start and at the end of the string.

$.trim("    hello, how are you?    ");

Result:

"hello, how are you?"
原创粉丝点击