在jquery中,trim()方法可以去除字符串两端的空白符号,包括换行符、空格符和制表符,语法“$.trim(string)”;参数“string”指定需要去除两端空白字符的字符串。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
在jquery中,trim()方法可以去除字符串两端的空白字符。
$.trim()函数会移除字符串开始和末尾处的所有换行符,空格(包括连续的空格)和制表符。如果空白字符是在字符串中间,则将被保留,不会被移除。
语法:
$.trim(string)
-
string:必须参数,String类型,指定需要去除两端空白字符的字符串。
jQuery 1.12.4下载jQuery 1.12.4是jQuery 1.x系列的最后一个正式稳定版本,由jQuery团队于2016年发布。该版本主要面向需要兼容旧版浏览器环境的网站和Web应用,尤其适用于仍需支持Internet Explorer 6、Internet Explorer 7、Internet Explorer 8等老旧浏览器的项目。
示例:
html> <meta> <script></script><pre class="brush:php;toolbar:false;"><script> $(function () { var str = " lots of spaces before and after "; $( "#original" ).html( "原始字符串: '" + str + "'" ); $( "#trimmed" ).html( "使用$.trim()'后: '" + $.trim(str) + "'" ); }) </script>

相关教程推荐:jQuery视频教程










