<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>


<script type="text/javascript">
                 function changerand(obj){  
           obj.src="images/image.jsp"+Math.random();  
          }  
</script>
  </head>
  
  <body>
      <center>
         <img src="images/image.jsp" id=randimg>  
<a href="#" onclick="changerand(document.getElementById('randimg'));">看不清,换一个</a> 
   </center>
  </body>
</html>
为什么我的验证码点击看不清楚会没图片了呢?我换成 href=“javascript:void(0)”;也是不动呀,也会没有的。什么原因,很纠结?

解决方案 »

  1.   

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'Test.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
         <script type="text/javascript">
            function changerand(obj){  
               obj.src="images/image.jsp"+Math.random();  
              }  
         </script>
      </head>
      
      <body>
        验证码:<input type="text" id="code" value=""/><img id="safecode1" src="images/image.jsp" style="width: 90px;height: 20px;" />
           <a href="#" onclick="javascript:this.setAttribute('src','images/image.jsp?'+Math.random())">看不清楚</a>
           <img src="images/image.jsp" id=randimg>  
    <a href="javascript:void(0);" onclick="changerand(document.getElementById('randimg'));">看不清,换一个</a> 
      </body>
    </html>
    下面这个也是不点击看不清楚后就没有反应?
      

  2.   

    obj.src="images/image.jsp?m="+Math.random();   
      

  3.   


     onclick="changerand(document.getElementById('randimg'));"等同于 onclick="changerand(this);"obj.src="images/image.jsp"+Math.random();   
    这个什么意思
    要知道Math.random();可是个小于1的随机数
    楼主的意思是不是向image.jsp传个参数,参数是1到10的随机数
    如果这样的话
    obj.src="images/image.jsp"+Math.random(); 改成
    obj.src="images/image.jsp?idx="+(int)(10*Math.random()+1)
    在image.jsp中
    request.getParmaeter("idx");
    就可以接收到传来的参数