【发布时间】:2020-04-20 18:30:34
【问题描述】:
有人可以向我解释如何显示 Array 中的元素吗?
我应该用什么来完成这个小的“推荐项目”。
目前,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;
}
};
【问题讨论】:
标签: javascript arrays function object onclick