gpt4 book ai didi

javascript - 在 JavaScript 中显示数组 onclick 中的下一项

转载 作者:行者123 更新时间:2023-12-02 21:01:40 25 4
gpt4 key购买 nike

有人可以向我解释如何显示数组中的元素吗?

我应该用什么来完成这个小型“推荐项目”。

目前,onclick 函数可以工作,但不合适:
-第二次点击后有效
-仅显示 Array 中的 2 条意见

<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;500&display=swap" rel="stylesheet">
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<!-- I used AI generated faces from this website: https://www.thispersondoesnotexist.com/image -->
<body>
<h3>CLIENT</h3>
<h1>TESTIMONIALS</h1>
<h5>Mercedes w124 coupe</h5>

<div class="container" data-index="0">
<div class="img" id="img"></div>
<p class="name" id="names"></p>
<p class="text" id="texts">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<i class='fas fa-quote-left' style='font-size:36px'></i>
<button class="left" onclick="next('left')"><i class='fas fa-angle-left'></i></button>
<button class="right" onclick="next('right')"><i class='fas fa-angle-right'></i></button>
</div>
</body>


var opinions = [
{ name: "Carol",
text: "Roomy and hard wearing inside, solid build quality that disguises miles well, still has class especially in estate and coupe form",
img: "https://www.thispersondoesnotexist.com/image"
},{
name: "Alex",
text: "Strong, reliable, comfortable, well-built, safe. Bigger, more modern car than W123.",
img: "https://www.thispersondoesnotexist.com/image"
},{
name: "Jordan",
text: "Extremely good looking coupe and convertible, with nice pillarless side window arrangement,solid build quality that disguises miles well, good ones are still capable of turning heads",
img: "https://www.thispersondoesnotexist.com/image"
}];

var element = document.getElementById("img");
var i = 0;
function next(direction) {
if(direction === 'right'){
element.style.backgroundImage ="url('"+ opinions[i++].img +"')";
document.getElementById("names").innerHTML = opinions[i].name;
document.getElementById("texts").innerHTML = opinions[i].text;
} else {
element.style.backgroundImage ="url('"+ opinions[i--].img +"')";
document.getElementById("names").innerHTML = opinions[i].name;
document.getElementById("texts").innerHTML = opinions[i].text;
}
};

最佳答案

在检查数组是否包含该元素之前,您将递增 i 的值。

而且,还有这一行

opinions[i++].img

第一次点击时您将获得options[1].img,因为您已将i定义为从0开始。

检查以下内容:

var opinions = [
{ name: "Carol",
text: "Roomy and hard wearing inside, solid build quality that disguises miles well, still has class especially in estate and coupe form",
img: "https://www.thispersondoesnotexist.com/image"
},{
name: "Alex",
text: "Strong, reliable, comfortable, well-built, safe. Bigger, more modern car than W123.",
img: "https://www.thispersondoesnotexist.com/image"
},{
name: "Jordan",
text: "Extremely good looking coupe and convertible, with nice pillarless side window arrangement,solid build quality that disguises miles well, good ones are still capable of turning heads",
img: "https://www.thispersondoesnotexist.com/image"
}];

var element = document.getElementById("img");
var i = -1; // start with negative value

function next(direction) {
if(direction === 'right') {
if(opinions[i+1]) { // check if element exists before incrementing i
i++;
element.style.backgroundImage ="url('"+ opinions[i].img +"')";
document.getElementById("names").innerHTML = opinions[i].name;
document.getElementById("texts").innerHTML = opinions[i].text;
}
} else {
if(opinions[i-1]) {
i--;
element.style.backgroundImage ="url('"+ opinions[i].img +"')";
document.getElementById("names").innerHTML = opinions[i].name;
document.getElementById("texts").innerHTML = opinions[i].text;
}
}
}
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;500&display=swap" rel="stylesheet">
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<script src='https://kit.fontawesome.com/a076d05399.js'></script>
<!-- I used AI generated faces from this website: https://www.thispersondoesnotexist.com/image -->

<h3>CLIENT</h3>
<h1>TESTIMONIALS</h1>
<h5>Mercedes w124 coupe</h5>

<div class="container" data-index="0">
<div class="img" id="img"></div>
<p class="name" id="names"></p>
<p class="text" id="texts">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<i class='fas fa-quote-left' style='font-size:36px'></i>
<button class="left" onclick="next('left')"><i class='fas fa-angle-left'></i></button>
<button class="right" onclick="next('right')"><i class='fas fa-angle-right'></i></button>
</div>

关于javascript - 在 JavaScript 中显示数组 onclick 中的下一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61329502/

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