douyi1966 2014-03-23 17:26
浏览 23

如何让3个Div从其自己的文件夹每秒更改图像?

I want to have 3 divs that changes its image every second.This code doesnt call any errors but the only one that is working is the 3rd div with source of script9.js. Why is this happening? Please help... Here it is:

I have 3 dives that contain images from there own folder:

<html>
<head>
  <script type="text/javascript" src="mainjavascript.js"></script>
  <script type="text/javascript" src="script8.js"></script>
  <script type="text/javascript" src="script9.js"></script>
</head>
<body onload = "startTimer()">

  <div><img id="img" src="pictures7/1.jpg">7</div>
  <div><img id="img2" src="pictures8/1.jpg">8</div>
  <div><img id="img3" src="pictures9/1.jpg">9</div>
</body>
</html>

I have 3 similar JavaScript files, one for each div that gets element by ID the only difference is in "** **" signs:

 function displayImage(image) {
          document.getElementById(**"img"**).src = image;
      }

      function displayNextImage() {
          x = (x == images.length - 1) ? 0 : x + 1;
          displayImage(images[x]);           
      }

      function displayPreviousImage() {
          x = (x <= 0) ? images.length - 1 : x - 1;
          displayImage(images[x]);           
      }

      function startTimer() {
          setInterval(displayNextImage, 1000);
      }

      var images = [], x = -1;
      images[0] = **"pictures7/"** + "1.jpg";
      images[1] = **"pictures7/"** + "2.jpg";
      for(var y=2;y<4;y++){
      images[y]= **"pictures7/"** + (y+1) + ".jpg";
      }

2nd div:

 function displayImage(image) {
          document.getElementById(**"img2"**).src = image;
      }

      function displayNextImage() {
          x = (x == images.length - 1) ? 0 : x + 1;
          displayImage(images[x]);           
      }

      function displayPreviousImage() {
          x = (x <= 0) ? images.length - 1 : x - 1;
          displayImage(images[x]);           
      }

      function startTimer() {
          setInterval(displayNextImage, 1000);
      }

      var images = [], x = -1;
      images[0] = **"pictures8/"** + "1.jpg";
      images[1] = **"pictures8/"** + "2.jpg";
      for(var y=2;y<4;y++){
      images[y]= **"pictures8/"** + (y+1) + ".jpg";
      }

3rd Div:

 function displayImage(image) {
          document.getElementById(**"img3"**).src = image;
      }

      function displayNextImage() {
          x = (x == images.length - 1) ? 0 : x + 1;
          displayImage(images[x]);           
      }

      function displayPreviousImage() {
          x = (x <= 0) ? images.length - 1 : x - 1;
          displayImage(images[x]);           
      }

      function startTimer() {
          setInterval(displayNextImage, 1000);
      }

      var images = [], x = -1;
      images[0] = **"pictures9/"** + "1.jpg";
      images[1] = **"pictures9/"** + "2.jpg";
      for(var y=2;y<4;y++){
      images[y]= **"pictures9/"** + (y+1) + ".jpg";
      }

Again, Only the image with id of "img3.jpg" is working... is it canceling the other code? should i use Jquery or some other code? Please help me.

  • 写回答

3条回答 默认 最新

  • douzhenchun6782 2014-03-23 17:35
    关注

    You're repeating the same function definitions several times; give them different names.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错