function Trim(obj,str,is_global){
var result;
result = str.replace(/(^\s+)|(\s+$)/g,"");
if(is_global.toLowerCase()=="all")
{
result = result.replace(/\s/g,"");
}
$(obj).val(result);
}
</script>
调用:当第三个参数为小写all的时候去除前后中间空格,否则只去除首尾空格
<input type="text" onblur="Trim(this,this.value,'all')" value="">