jQuery.trim()方法
$.trim()方法作用是删除一个字符串的开头和结尾的空白(包括换行、空格等等)。
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.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>
结果:
使用方法:
$.trim(" hello, how are you? ");
结果:
"hello, how are you?"