1
第一种:
2
String.prototype.lTrim = function(){
3
return this.replace(/^\s*/,"");
4
};//去掉左面空格;
5
String.prototype.rTrim = function(){
6
return this.replace(/\s*$/,"");
7
};//去掉右面空格;
8
String.prototype.Trim = function(){
9
return this.lTrim().rTrim();
10
};//记得各句后都有分号
11
12
第二种:
13
function lTrim(str){
14
if(str.charAt(0)==""){
15
str = str.slice(1);
16
str.lTrim();
17
}
18
return str;
19
}
20
21
function rTrim(str){
22
if(str.charAt(str.length-1)==""){
23
str = str.slice(0,str.length-1);
24
str.rTrim();
25
}
26
return str;
27
}
28
29
function trim(str){
30
return rTrim(ltrim(str));
31
}
以上代码供初学者学习。是javascript脚本中最基础的函数
第一种:2
String.prototype.lTrim = function(){3
return this.replace(/^\s*/,"");4
};//去掉左面空格;5
String.prototype.rTrim = function(){6
return this.replace(/\s*$/,"");7
};//去掉右面空格;8
String.prototype.Trim = function(){9
return this.lTrim().rTrim();10
};//记得各句后都有分号11

12
第二种:13
function lTrim(str){14
if(str.charAt(0)==""){15
str = str.slice(1);16
str.lTrim();17
}18
return str;19
}20

21
function rTrim(str){22
if(str.charAt(str.length-1)==""){23
str = str.slice(0,str.length-1);24
str.rTrim();25
}26
return str;27
}28

29
function trim(str){30
return rTrim(ltrim(str));31
},自我感觉第一种比较好,采用原型方式。望各位发表高见
大部分转载 小部分自写
String.prototype.lTrim 
浙公网安备 33010602011771号