jQuery.trim()
去掉字符串起始和结尾的空格。
jQuery.trim(str)
- str类型: String。被处理的字符串
$.trim()
函数会移除字符串开始和结尾处的所有换行符,空格(包括连续的空格)和制表符(tab)。如果这些空白字符在字符串中间时,它们将被保留,不会被移除。
注意:从jquery3.5 开始 jQuery.trim()抛弃使用。请使用String.prototype.trim
方法过滤两端的空格。
例子
删除在开始和在字符串的末尾两个空格。
<!DOCTYPE html> <html> <head> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.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? ");
Result:"hello, how are you?"