:D
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>去空格</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
//去两边空格
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
};
//去所有空格
String.prototype.trimAll = function(){
return this.replace(/(^\s*)|(\s*)|(\s*$)/g, "");
};
function PeripheralTrim(){
var tmp = document.getElementById("inputid").value.trim();
document.getElementById("inputid1").value = tmp;
}
function AllTrim(){
var tmp = document.getElementById("inputid").value.trimAll();
document.getElementById("inputid1").value = tmp;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<input type="button" onclick="PeripheralTrim()" value="去两边空格"/><br>
<input type="button" onclick="AllTrim()" value="去所有空格"/><br>
<input type="text" id="inputid"/><br>
<input type="text" id="inputid1"/>
</BODY>
</HTML>
本文介绍了一种使用JavaScript实现的字符串去空格方法,包括去除字符串两端的空格及去除字符串中的所有空格。通过定义字符串原型方法trim和trimAll来实现功能,并提供了具体的HTML页面示例代码。

被折叠的 条评论
为什么被折叠?



