error: object doesn't support this property or method使用trim()方法去掉空格,
debugger 指到
trim() ltrim() rtrim()方法的 while(str.charAt(idx).search(/\s/)==0)ltrim() 方法如下:
//删除字符串左边的空格
function ltrim(str) { 
if(str.length==0)
return(str);
else {
var idx=0;
while(str.charAt(idx).search(/\s/)==0)
idx++;
return(str.substr(idx));
}
}怎么解决这个错误,谢谢了

解决方案 »

  1.   

    function ltrim(str) {
      return str.replace(/^\s+/, "");
    } function rtrim(str) {
      return str.replace(/\s+$/, "");
    } function trim(str) {
      return str.replace(/^\s+|\s+$/g, "");
      

  2.   


    <script type="text/javascript">
    <!--
    // 去左空格
    String.prototype.ltrim = function(){
    return this.replace(/^\s+/,"");
    }
    // 去右空格
    String.prototype.rtrim = function(){
    return this.replace(/\s+$/,"");
    }
    // 去左右空格
    String.prototype.trim = function(){
    return this.replace(/^\s+|\s+$/g,"");
    }
    alert("                                  ff".ltrim());
    alert("ff                                  ".rtrim());
    alert("           ff                       ".trim());
    //-->
    </script>