<html>
<head>
<script>
function trim(str) {
for ( var i = 0; i < str.length && str.charAt(i) == " "; i++)
;
for ( var j = str.length; j > 0 && str.charAt(j - 1) == " "; j--)
;
if (i > j)
return "";
return str.substring(i, j);
}
function check(){
alert("-"+document.getElementById('myid').value+"-");
alert("-"+trim(document.getElementById('myid').value)+"-");
}
</script>
</head>
<body>
<input type="text" id="myid" value=" 空 格 " onclick="javascript:check()"/>
</body>
</html>
JS中trim()方法
最新推荐文章于 2024-07-01 12:34:45 发布