今天又自学了javascript,看到了一个window对象的定时器,于是突发奇想,想自己做一个类似于gif动态图片的链接,大型网站上都有的,过一会翻过去一张图片,点击图片能链接到相应的页面那种。但是,又出现出错了,代码如下:<%@ 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>
  <script type="text/javascript" >
   var inc=0;
   var len=3;
   var Href="";
   var Src="";
   var timeoutID;
   function SetLink(){
   form1.ad.href=Href;
   form1.img.src=Src;
   }
   function xxp(){
   if(inc>len)
    inc=1;
   else
    inc++;
   if(inc==1){
   Href="http://www.baidu.com";
   Src="PICTURE/1.jpg";
   }
   if(inc==2){
   Href="http://www.163.com";
   Src="PICTURE/2.jpg";
   }
   if(inc==3){
   Href="http://www.google.com";
   Src="PICTURE/3.jpg";
   }
   SetLink();
   timeoutID=setTimeout("xxp()",2000);
  
   }
   }
    </script>
    <base href="<%=basePath%>">
     <title>TEST</title>
</head>
  <body onload="xxp()"  onunload="clearTimeout(timeoutID)">
  <form name="form1"  >
   <a name="ad"  ><img name="img" > </a>
  
 </form>
  </body>
</html>
写的比较简单,图片的路径也是对的,在别的页面中测试过。大家帮我看看,拜托了,请提供正确的答案,我会按答案的正确性给分,谢谢了。

解决方案 »

  1.   


    <script type="text/javascript" >
          var inc=0;
          var len=3;
          var Href="";
          var Src="";
          var timeoutID;
          function SetLink(){
    //  A 标签不能那样子用, 呵呵,
              document.getElementsByTagName("a")[0].href=Href;
              form1.img.src=Src;
          }
          function xxp(){
          
              if(inc>len)
               inc=1;
              else
               inc++;
              if(inc==1){
                  Href="http://www.baidu.com";
                  Src="PICTURE/1.jpg";
              }
              if(inc==2){
                  Href="http://www.163.com";
                  Src="PICTURE/2.jpg";
                  }
              if(inc==3){
                  Href="http://www.google.com";
                  Src="PICTURE/3.jpg";
                  }
              SetLink();
              timeoutID=setTimeout("xxp()",2000);
              
             // 你这里多了个 }
          }
        </script>
      

  2.   

    多谢这么大哥,按照你说的已经解决了,我会马上结贴的,别的大侠就不用回复了。还有一点啊,这个a标签为什么就特别一点不能直接引用里面的href属性?求大哥解答。(可能我还没看到这一块吧,嘿嘿)
      

  3.   

    a标签你设个id也可能可以的,用document.getelementbyid('ad')。设name吧,用getelementsbyname('ad')[0]应该可以吧。
    什么都不设,那就是2楼说的getelementsbytagname('a')[0]。