Javascript 下一张和上一张图像

标签 javascript jquery html

代码应该在单击下一个箭头时显示下一个图像,在单击上一个箭头时显示上一个图像。但它不起作用。 (分配 img.src=imgs[this.i] 时发生错误;它说 Cannot set property 'src' of null 在collection.next)。

Javascript代码:

var arr = new collection(['cake.png', 'image2.png', 'image3.png', 'image1.png']);

function collection(imgs) {
  this.imgs = imgs;
  this.i = 0;
  this.next = function(element) {
    var img = document.getElementById('element')
    this.i++;
    if (this.i == imgs.length) {
      this.i = 0;
    }
    img.src = imgs[this.i].src;


  }
  this.prev = function(element) {
    var img = document.getElementById('element');
    this.i--;
    if (this.i < 0) {
      this.i = imgs.length - 1;
    }
    img.src = imgs[this.i].src;
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>photos</title>
  <script src="photos.js"></script>
</head>

<body>

  <input type='button' value='<' name='next' onclick="arr.next('mainImg')" />
  <img id='mainImg' src="cake.png">
  <input type='button' value='>' name='prev' onclick="arr.prev('mainImg')" />


</body>

</html>

不使用jquery。我对js也没有足够的经验。感谢您的宝贵时间

最佳答案

你犯了三个错误:

  1. 您将图像引用为 img.src = imgs[this.i].src;并且您只有一个字符串数组,而不是带有 src 的对象数组属性(property)。 img.src = imgs[this.i];是获取URL的正确方法。
  2. 您使用了

    var img = document.getElementById('element');
    

    什么时候你应该使用

    var img = document.getElementById(element);
    

    element是来自您的 onclick 的一个论点事件。它拥有 id您应该使用的图像。 "element"只是一个字符串。您尝试查找 id 的元素等于 element这是不存在的。

  3. 编辑:您还应该使用 &lt;&gt;代表<> 。否则你的 HTML 可能会搞砸。更多信息here .

var arr = new collection(['http://images.math.cnrs.fr/IMG/png/section8-image.png', 'https://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/jpeg444.jpg', "http://saturnraw.jpl.nasa.gov/multimedia/images/raw/casJPGFullS72/N00183828.jpg"]);

function collection(imgs) {
  this.imgs = imgs;
  this.i = 0;

  this.next = function(element) {
    var img = document.getElementById(element);
    
    this.i++;
    if (this.i >= imgs.length) {
      this.i = 0;
    }

    img.src = imgs[this.i];
  };
 
  this.prev = function(element) {
    var img = document.getElementById(element);
  
    this.i--;
    if (this.i < 0) {
      this.i = imgs.length - 1;
    }
  
    img.src = imgs[this.i];
  };
  
  this.next("mainImg"); // to initialize with some image
}
<!DOCTYPE html>
<html>

<head>
  <title>photos</title>
  <script src="photos.js"></script>
</head>

<body>

  <input type='button' value='<' name='next' onclick="arr.next('mainImg')" />
  <img id='mainImg' src="cake.png">
  <input type='button' value='>' name='prev' onclick="arr.prev('mainImg')" />


</body>

</html>

这就是我个人的做法:

var myCollection = new Collection([
  "http://images.math.cnrs.fr/IMG/png/section8-image.png",
  "https://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/jpeg444.jpg",
  "http://saturnraw.jpl.nasa.gov/multimedia/images/raw/casJPGFullS72/N00183828.jpg"
], "mainImg");

document.getElementById("next_btn").onclick = function() {
  myCollection.next();
};

document.getElementById("prev_btn").onclick = function() {
  myCollection.prev();
}

function Collection(urls, imgID) {
  var imgElem = document.getElementById(imgID);
  var index = 0;

  this.selectImage = function() {
    imgElem.src = urls[index];
  };

  this.next = function() {
    if (++index >= urls.length) {
      index = 0;
    }

    this.selectImage();
  };

  this.prev = function(element) {
    if (--index < 0) {
      index = urls.length - 1;
    }

    this.selectImage();
  };

  // initialize
  this.selectImage();
}
<!DOCTYPE html>
<html>

<head>
  <title>photos</title>
  <script src="photos.js"></script>
</head>

<body>
  <input id="next_btn" type='button' value='&lt;' />
  <img id='mainImg'>
  <input id="prev_btn" type='button' value='&gt;' />
</body>

</html>

关于Javascript 下一张和上一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454306/

相关文章:

jquery - 使用 JS SDK 和 Ajax 保存用户个人资料信息会抛出代码 190

java - 在JSP中显示图像(html)

javascript - Bootstrap 3 Mobile Forms 自动调整大小不起作用

javascript - 无法在 React Native 中导入其他组件

javascript - 使用正则表达式在较大字符串的开头查找字符串模式?

javascript - 递归异步 api 调用

jQuery 验证小于

javascript - 在 Chrome 中重新抛出错误以调试 javascript 的更好方法

javascript - 防止ajax中的特殊字符(http)

javascript - 在 CSS 中按半像素移动的图像插值