gpt4 book ai didi

Javascript 下一张和上一张图像

转载 作者:行者123 更新时间:2023-11-28 14:57:09 25 4
gpt4 key购买 nike

代码应该在单击下一个箭头时显示下一个图像,在单击上一个箭头时显示上一个图像。但它不起作用。 (分配 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com