您的位置:首页 > Web前端 > JQuery

jquery删除字符串空格trim()函数

2013-09-05 20:23 375 查看
jquery.trim( str )
//返回字符串;
//从字符串的头部和尾部移除空格。
//从字符串的头部和尾部移除空格、换行符、制表符。字符串“中部(中间)”的空格、换行符、制表符是不移出的。
//jquery的换行符是 n

// used for trimming whitespace
trimleft = /^s+/,
trimright = /s+$/,
// use native string.trim function wherever possible
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} :
// otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
text.tostring().replace( trimleft, "" ).replace( trimright, "" );
},
看一个实例应用
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
<button>show trim example</button>
<script>
$("button").click(function () {
var str = " lots of spaces before and after ";
alert("'" + str + "'");
str = jquery.trim(str);
alert("'" + str + "' - no longer");
});
</script>
</body>
</html>
从上面实例中可以看出jquery.trim函数其实与js中的trim是很像的,我记得前天我还写过一个php教程 trim函数它们都大同小义了,好了费话就说到这里。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐