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 閱讀(952) 評論(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

          搜索

          •  

          積分與排名

          • 積分 - 917240
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 洛扎县| 霍山县| 安宁市| 吉隆县| 湖口县| 桐柏县| 岐山县| 梁山县| 呼和浩特市| 梨树县| 奇台县| 五大连池市| 闻喜县| 招远市| 新郑市| 磐安县| 兴隆县| 五原县| 青铜峡市| 福安市| 仙居县| 怀柔区| 三台县| 柘荣县| 昌吉市| 社旗县| 泊头市| 美姑县| 曲周县| 鄢陵县| 马边| 通州市| 涪陵区| 呼和浩特市| 共和县| 桐城市| 衡阳市| 德庆县| 始兴县| 礼泉县| 梓潼县|