1 <SCRIPT LANGUAGE="JavaScript">    
 2 String.prototype.trim = function()
 3 {
 4     return this.replace(/(^\s*)|(\s*$)/g, "");
 5 }
 6 function test(te)
 7 {
 8     alert (te.trim());
 9     alert(te.length);
10     alert(te.trim().length);
11     
12 }
13 </SCRIPT>
14 <html>
15 <body>
16 <form name="form1" method="post">
17   <p>
18     <input type="text"  id ="te" name="textfield" >
19   </p>
20   <p>
21     <input type="button" name="Submit" value="提交"  onClick="test(document.getElementById('te').value)">
22   </p>
23 </form>
24 </body>
25 </html>
26 
posted on 2007-02-26 16:27 -274°C 阅读(931) 评论(3)  编辑  收藏 所属分类: web前端


FeedBack:
# re: 去除空格[未登录]
2007-02-27 23:04 | 阿蜜果
我试了下好像不行哦,将return this.replace(/(^\s*)|(\s*$)/g, "");
改成return this.replace(/\s+/g, "");就可以了
:)  回复  更多评论
  
# re: 去除空格
2007-02-28 09:28 | JAVA-HE
我试的时候可以的,对了我的是去除前后的空格,不是所有空格。  回复  更多评论
  
# re: 去除空格
2009-03-06 17:36 | sure_xx
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, "");
}
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909059
  • 排名 - 40

最新评论