随笔 - 147  文章 - 71  trackbacks - 0
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔分类(146)

随笔档案(147)

文章分类(28)

文章档案(28)

喜欢的Blog

搜索

  •  

最新评论

阅读排行榜

评论排行榜

    利用Javascript中每个对象(Object)的prototype属性我们可以为Javascript中的内置对象添加我们自己的方法和属性。以下就用这个属性来为String对象添加三个方法:Trim,LTrim,RTrim(作用和VbScript中的同名函数一样)
 1String.prototype.Trim = function()
 2{
 3  return this.replace(/(^\s*)|(\s*$)/g, "");
 4}

 5String.prototype.LTrim = function()
 6{
 7  return this.replace(/(^\s*)/g, "");
 8}

 9String.prototype.Rtrim = function()
10{
11  return this.replace(/(\s*$)/g, "");
12}
    使用的实例:
 1<script language=javascript>
 2String.prototype.Trim = function()
 3{
 4  return this.replace(/(^\s*)|(\s*$)/g, "");
 5}
 
 6var s = "   leading and trailing spaces   ";
 7window.alert(s + " (" + s.length + ")"); 
 8= s.Trim(); 
 9window.alert(s + " (" + s.length + ")");
10</script>
posted on 2009-03-24 15:29 飞翔天使 阅读(260) 评论(0)  编辑  收藏 所属分类: javascript

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


网站导航: