js去除空格,去除左右两端的空格
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年1月7日 14:26:28 星期三
更多请关注:http://fanshuyao.iteye.com/
?
//去除左右两端的空格
function trim(str) {
if(str == null){
str = "";
}
return str.replace(/(^\s*)|(\s*$)/g, "");
};
//去除所有空格
function removeSpace(str){
if(str == null){
str = "";
}
return str.replace(/\s/g, "");
};
/**去除Input的所有空格
**id input的id属性
*/
function removeInputSpace(id){
if(id == null || id == ""){
alert("传入的id值不能为空");
}else{
var tempObject = document.getElementById(id);
if(tempObject == null){
alert("不存在id值为"+id+"的对象");
}else{
tempObject.value = removeSpace(tempObject.value);
}
}
};
?
onkeyup="removeInputSpace(‘id属性‘);"
这个方法会自动去掉input的空格
?
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2015年1月7日 14:26:28 星期三
更多请关注:http://fanshuyao.iteye.com/
原文:http://fanshuyao.iteye.com/blog/2173566