当前位置: 代码迷 >> JavaScript >> javascript中去除左右空格解决方案
  详细解决方案

javascript中去除左右空格解决方案

热度:162   发布时间:2012-03-09 16:54:57.0
javascript中去除左右空格
RT:求在javascript中去除左右空格的写法

if(leftTrim(rightTrim(document.getElementById( "TextBox1 ").value))   ==   " ")
{
alert( '暗暗啊 ');
}

提示缺少对象

------解决方案--------------------
//函数
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, " ");}
String.prototype.ltrim = function(){return this.replace(/^\s*/, " ");}
String.prototype.rtrim = function(){return this.replace(/\s*$/, " ");}

使用
" abc ".trim();
" abc ".ltrim();
"abc ".rtrim();
  相关解决方案