JS中trim()方法


<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>