完整程序如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function IsNull(){    
     var str = document.getElementById('str').value.trim();    
     if(str.length==0){   
     document.getElementById('1').innerText="对不起,文本框不能为空或者为空格!"; 
        }
    }
    </script>    
  </head>
  
  <body>
  <form>
   <tr>
   <td>姓名</td>
   <input type="text" id="str"  onblur="IsNull()"/>
   </tr>
   <tr>
   <div id="1">
   </div>
   </tr>
  </form>
  </body>
</html>

解决方案 »

  1.   

    js没有trim方法,需要自己写一个,这个是我从网上找的一个,试试看好用否。function IsNull(){    
        var str = trim(document.getElementById('str').value);    
        if(str.length==0){    
            alert('对不起,姓名不能为空或者为空格!');  
        }    

    function  trim(str)
    {
        str = str.replace(/ /g,' ');
        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);  
    }  
      

  2.   

    不等于空就行了啊
    if(str!="")
      

  3.   

    function IsNull(){    
        var str = document.getElementById('str').value.replace(/\s/g,"");    
        if(str.length==0){    
            alert('对不起,姓名不能为空或者为空格!');  
        }    
    }    
      

  4.   

    二楼和四楼的方法确实可行,谢谢了,能不能具体讲解一下replace(/\s/g,"")这个方法
      

  5.   

    string.replace(/\s/g,"")replace方法是一个把前一个参数替换成后一个参数,/\s/g,是一个正则表达式,匹配所有的空白字符value.replace(/\s/g,"");就是把value的内容里面所有的空白字符替换为空,即去掉所有的空白字符
      

  6.   

    js不支持trim()方法可能VBS支持trim()方法