当前位置:编程学习 > JS >>

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

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函数它们都大同小义了,好了费话就说到这里。

补充:网页制作,jquery 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,