首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助循环遍历附加在图像上的标题

需要帮助循环遍历附加在图像上的标题
EN

Stack Overflow用户
提问于 2022-10-06 02:05:08
回答 1查看 65关注 0票数 0

我需要创建一个for循环,其计数器从0到小于标题数组的长度为1。每次迭代时,将以下文本添加到htmlCode变量的值中:

代码语言:javascript
复制
<figure>
<img alt='' src='slidei.jpg' />
<figcaption>caption[i]</figcaption>
</figure>

其中,i是该迭代的计数器的值,captionsi是标题数组中的对应元素。如果有人能帮我的话,我已经坚持了好几天了。

代码语言:javascript
复制
    let captions = new Array(14);
captions[0] = "International Space Station fourth expansion [2009]";
captions[1] = "Assembling the International Space Station [1998]";
captions[2] = "The Atlantis docks with the ISS [2001]";
captions[3] = "The Atlantis approaches the ISS [2000]";
captions[4] = "The Atlantis approaches the ISS [2000]";
captions[5] = "International Space Station over Earth [2002]";
captions[6] = "The International Space Station first expansion [2002]";
captions[7] = "Hurricane Ivan from the ISS [2008]";
captions[8] = "The Soyuz spacecraft approaches the ISS [2005]";
captions[9] = "The International Space Station from above [2006]";
captions[10] = "Maneuvering in space with the Canadarm2 [2006]";
captions[11] = "The International Space Station second expansion [2006]";
captions[12] = "The International Space Station third expansion [2007]";
captions[13] = "The ISS over the Ionian Sea [2007]";

var htmlCode = "";

for (let i = 0; i < captions.length; i++) {
  <figure>
    <img alt='' src='slidei.jpg' />
    <figcaption>htmlCode += captions[i]</figcaption>
  </figure>
}

document.getElementById(gallery).innerHTML = htmlCode;
EN

回答 1

Stack Overflow用户

发布于 2022-10-06 02:15:54

有两点需要注意:

  1. 使用document.getElementById('gallery')而不是document.getElementById(gallery)
  2. 您可以使用文字存储interate值,并最终将其分配给gallery外部循环。

代码语言:javascript
复制
let captions = new Array(14);
captions[0] = "International Space Station fourth expansion [2009]";
captions[1] = "Assembling the International Space Station [1998]";
captions[2] = "The Atlantis docks with the ISS [2001]";
captions[3] = "The Atlantis approaches the ISS [2000]";
captions[4] = "The Atlantis approaches the ISS [2000]";
captions[5] = "International Space Station over Earth [2002]";
captions[6] = "The International Space Station first expansion [2002]";
captions[7] = "Hurricane Ivan from the ISS [2008]";
captions[8] = "The Soyuz spacecraft approaches the ISS [2005]";
captions[9] = "The International Space Station from above [2006]";
captions[10] = "Maneuvering in space with the Canadarm2 [2006]";
captions[11] = "The International Space Station second expansion [2006]";
captions[12] = "The International Space Station third expansion [2007]";
captions[13] = "The ISS over the Ionian Sea [2007]";

var htmlCode = "";

for (let i = 0; i < captions.length; i++) {
  htmlCode += `<figure>
    <img alt='' src='slidei.jpg' />
    <figcaption>htmlCode += `+captions[i]+`</figcaption>
  </figure>`
}

document.getElementById('gallery').innerHTML = htmlCode;
代码语言:javascript
复制
<div id="gallery"></div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73967993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档