微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery.trim() 函数及trim()用法详解

jQuery.trim()函数用于去除字符串两端的空白字符。该函数可以去除字符串开始和末尾两端的空白字符(直到遇到第一个非空白字符串为止)。它会清除包括换行符、空格、制表符等常见的空白字符。

函数属于全局的jQuery对象。

语法

jQuery 1.0 新增该静态函数

jQuery.trim( str )

ottom: rgb(128,128,128) 1px solid; border-left: rgb(128,128) 1px solid; padding-bottom: 0px; overflow-x: auto; text-transform: none; background-color: transparent; text-indent: 0px; margin: 1.5em 0px; outline-style: none; outline-color: invert; padding-left: 0px; outline-width: 0px; border-spacing: 0px; letter-spacing: normal; padding-right: 0px; border-collapse: collapse; font: 13px/19px 'Courier New',宋体,'Times New Roman',serif; white-space: normal; color: rgb(51,51,51); vertical-align: baseline; border-top: rgb(128,128) 1px solid; border-right: rgb(128,128) 1px solid; word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; background-color: transparent; margin: 0px; outline-style: none; outline-color: invert; padding-left: 0px; outline-width: 0px; padding-right: 0px; font-size: 13px; vertical-align: baseline; border-top: 0px; border-right: 0px; padding-top: 0px"> irstRow" style="border-bottom: 0px; border-left: 0px; padding-bottom: 0px; background-color: rgb(238,238,238); margin: 0px; outline-style: none; outline-color: invert; padding-left: 0px; outline-width: 0px; padding-right: 0px; font-size: 13px; vertical-align: baseline; border-top: 0px; border-right: 0px; padding-top: 0px"> ottom: 0px; border-left: 0px; padding-bottom: 0px; background-color: transparent; margin: 0px; outline-style: none; outline-color: invert; padding-left: 0px; outline-width: 0px; padding-right: 0px; font-size: 13px; vertical-align: baseline; border-top: 0px; border-right: 0px; padding-top: 0px">
ottom: rgb(132,132,132) 1px solid; text-align: left; border-left: rgb(128,128) 1px solid; padding-bottom: 5px; background-color: transparent; margin: 0px; outline-style: none; outline-color: invert; padding-left: 5px; outline-width: 0px; padding-right: 5px; white-space: Nowrap; font-size: 13px; vertical-align: top; border-top: rgb(128,128) 1px solid; padding-top: 5px">参数 ottom: rgb(132,128) 1px solid; padding-top: 5px">描述 ottom: rgb(128,128) 1px solid; padding-bottom: 5px; background-color: transparent; margin: 0px; outline-style: none; outline-color: invert; padding-left: 5px; outline-width: 0px; padding-right: 5px; font-size: 13px; vertical-align: top; border-top: rgb(128,128) 1px solid; padding-top: 5px">strottom: rgb(128,128) 1px solid; padding-top: 5px">ottom: 0px; border-left: 0px; padding-bottom: 0px; background-color: rgb(125,144,8); font-style: normal; margin: 0px 5px 0px 0px; outline-style: none; outline-color: invert; padding-left: 3px; outline-width: 0px; padding-right: 3px; display: inline-block; color: rgb(238,255,255); font-size: 12px; vertical-align: baseline; border-top: 0px; border-right: 0px; padding-top: 0px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px">String类型需要去除两端空白字符的字符串。

如果参数str不是字符串类型,该函数自动将其转为字符串(一般调用其toString()方法)。如果参数str为null或undefined,则返回空字符串("")。

返回值

jQuery.trim()函数的返回值为String类型,返回去除两端空白字符串后的字符串。

示例&说明

以下是与jQuery.trim()函数相关的jQuery示例代码,以演示jQuery.trim()函数的具体用法

rush:js;"> //在当前页面内追加换行标签和指定的HTML内容 function w( html ){ document.body.innerHTML += "
" + html; } // 输出两侧添加双引号,以便于区分字符串边界 w( '"' + $.trim( " CodePlayer " ) + '"'); // "CodePlayer" // 只会去除两端的连续空白字符 w( '"' + $.trim( " Code Player " ) + '"'); // "Code Player" w( '"' + $.trim( "\r\n\t CodePlayer \t" ) + '"'); // "CodePlayer" w( '"' + $.trim( "" ) + '"'); // "" w( '"' + $.trim( 12 ) + '"'); // "12" w( '"' + $.trim( null ) + '"'); // "" w( '"' + $.trim( undefined ) + '"'); // "" w( '"' + $.trim( new Object() ) + '"'); // "[object Object]"

jquery中trim()的用法

rush:js;"> $.trim()

如果你在IE8浏览器下开发网站,其实这是个假命题,因为原生的javascript 并不支持 .trim()方法,如果你写了类似document.getElementByID().trim();的代码,在IE8模式下运行,会报 : 不支持方法属性错误

 解决方法如下所示:

1.使用JQuery提供的$.trim([要操作的对象]);

2.自己封装一个方法进行空白字符的截取

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐