1.js中本身是沒有trim函數(shù)的,不過你可以自己寫一個(gè)
復(fù)制代碼 代碼如下:
function trim(str) {
var newStr = str.replace(/^/s*$/g,'')
retrun newStr;
}
復(fù)制代碼 代碼如下:
<script language="javascript">
String.prototype.trim=function(){
return this.replace(/(^/s*)|(/s*$)/g, "");
}
String.prototype.ltrim=function(){
return this.replace(/(^/s*)/g,"");
}
String.prototype.rtrim=function(){
return this.replace(/(/s*$)/g,"");
}
</script>
寫成函數(shù)可以這樣:(trim(str))
<script type="text/javascript">
function trim(str){ //刪除左右兩端的空格
return str.replace(/(^/s*)|(/s*$)/g, "");
}
function ltrim(str){ //刪除左邊的空格
return str.replace(/(^/s*)/g,"");
}
function rtrim(str){ //刪除右邊的空格
return str.replace(/(/s*$)/g,"");
}
</script>
新聞熱點(diǎn)
疑難解答
圖片精選