<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function()
{
var aInput=document.getElementsByTagName("input");
var oT=aInput[0];
//去掉左空格:
function trimL(str)
{
var reg=/^s+/;
return str.replace(reg,'');
}
//去掉右空格:
function trimR(str)
{
var reg=/s+$/;
return str.replace(reg,'');
}
//去掉全部的空格:
function trim(str)
{
return str.replace(/^s+|s+$/g,'').replace(/s+/g,' ');
}
aInput[1].onclick=function()
{
alert("|"+trim(oT.value)+"|");
}
}
</script>
</head>
<body>
<h1>去掉空格</h1>
<input type="text">
<input type="button" value="检測" >
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function()
{
var aInput=document.getElementsByTagName("input");
var oT=aInput[0];
//去掉左空格:
function trimL(str)
{
var reg=/^s+/;
return str.replace(reg,'');
}
//去掉右空格:
function trimR(str)
{
var reg=/s+$/;
return str.replace(reg,'');
}
//去掉全部的空格:
function trim(str)
{
return str.replace(/^s+|s+$/g,'').replace(/s+/g,' ');
}
aInput[1].onclick=function()
{
alert("|"+trim(oT.value)+"|");
}
}
</script>
</head>
<body>
<h1>去掉空格</h1>
<input type="text">
<input type="button" value="检測" >
</body>
</html>