简体   繁体   English

我无法获得实际的元素数量

[英]I can't get the actual number of elements

I'm having a problem returning elements from my object array.我在从 object 数组返回元素时遇到问题。

I have a slideshow in which an image (photo of the book) and a text (title of the book) must appear.我有一个幻灯片,其中必须出现图像(书的照片)和文本(书名)。

For the creation of the slideshow I have no problems because I am using querySelectors as shown in the code below.对于幻灯片的创建,我没有问题,因为我使用的是 querySelectors,如下面的代码所示。

The problem lies in showing the images with their titles.问题在于显示带有标题的图像。

In the first part of the code I have an array of objects that gives me the total number of elements present and based on this I create the slideshow (to create the scroll points and the "container" to contain the images) and then subsequently I call the function myFunction with id argument to return the single element (identified by an id).在代码的第一部分中,我有一个对象数组,它给出了存在的元素总数,并在此基础上创建了幻灯片(创建滚动点和包含图像的“容器”),然后我使用 id 参数调用 function myFunction 以返回单个元素(由 id 标识)。

What I notice is that the urls with the ids are returned to me, but the url with that id is returned multiple times (so it gets copied) and not just once as expected;我注意到带有 id 的 url 被返回给我,但是带有该 id 的 url 被多次返回(所以它被复制),而不是像预期的那样只返回一次; I should have only 4 url in total (each with the specific id of the element, since only 4 elements).我总共应该只有 4 个 url (每个都有元素的特定 id,因为只有 4 个元素)。 The same happens when I return the array of objects, it does not return 4 but many more.当我返回对象数组时也会发生同样的情况,它不返回 4,而是返回更多。 (Below) (以下)

Referring to url1:参考url1:

.../books/123456 .../书籍/123456

.../books/123456 .../书籍/123456

.../books/135623 .../书籍/135623

.../books/123456 .../书籍/123456

.../books/135623 .../书籍/135623

.../books/123789 .../书籍/123789

.../books/123456 .../书籍/123456

.../books/135623 .../书籍/135623

.../books/123789 .../书籍/123789

.../books/146975 .../书籍/146975

If I click on the arrows of the slideshow I only receive one image, the other images I do not receive.如果我点击幻灯片的箭头,我只会收到一张图片,其他图片我不会收到。

I have no mistakes.我没有错误。

 var slideIndex = 1; var outerXmlhttp = new XMLHttpRequest(); var url = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books"; outerXmlhttp.onreadystatechange = function () { var innerXmlhttp; if (this.readyState == 4 && this.status == 200) { var allbook = JSON.parse(this.responseText); for (var i = 0, len = allbook.Items.length; i < len; i++) { id = allbook.Items[i].id document.querySelector('.slideshow-container').innerHTML += ` <div class="mySlides fade"> <div class="numbertext">${i+1}/${allbook.Items.length}</div> <img id="img" src onerror="this.onerror=null; this.src=myFunction(${id});" style="width:100%"> <div class="text" id="title" onclick="myFunction(${id})"></div> </div>`; document.querySelector('#punt').innerHTML += ` <span class="dot" onclick=currentSlide(${i+1})></span>`; showSlides(slideIndex); myFunction(id); } } }; outerXmlhttp.open("GET", url, true); outerXmlhttp.send(); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides fade"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } function myFunction(id) { var url1 = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books/" + id; innerXmlhttp = new XMLHttpRequest(); innerXmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { var myArr = JSON.parse(this.responseText); document.getElementById("img").src = "book_img/" + myArr.Item.immagine; document.getElementById("title").innerHTML = `<a href="fragmentProva.html?id=${id}">${myArr.Item.titolo}</a>`; } }; innerXmlhttp.open("GET", url1, true); innerXmlhttp.send(); }
 <div class="slideshow-container" id="slideshow"> <a class="prev" onclick="plusSlides(-1)">&#10094;</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <div id="punt" style="text-align:center"> </div>

EDIT编辑

Array allbook with error:数组 allbook 有错误: 在此处输入图像描述

Several things I noticed:我注意到了几件事:

  1. Doesn't define the id没有定义id
  2. As you state, the data structure from api will look like {"Item":{"marca":"Love","titolo":"You will be missed indefinitely","id":"123456","immagine":"ind.jpg","data":"27/11/2021"}} , but are trying to loop over all the item inside item which will be incorrect, you should loop over allbook .当您使用 state 时,来自 api 的数据结构将看起来像{"Item":{"marca":"Love","titolo":"You will be missed indefinitely","id":"123456","immagine":"ind.jpg","data":"27/11/2021"}} ,但试图循环遍历 item 内的所有不正确的项目,您应该遍历allbook
  3. It should be allbook[i].Items.length instead of allbook.Items.length它应该是allbook[i].Items.length而不是allbook.Items.length

 var slideIndex = 1; let id; var outerXmlhttp = new XMLHttpRequest(); var url = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books"; outerXmlhttp.onreadystatechange = function() { var innerXmlhttp; if (this.readyState == 4 && this.status == 200) { var allbook = JSON.parse(this.responseText); for (var i = 0, len = allbook.length; i < len; i++) { id = allbook[i].Items.id document.querySelector('.slideshow-container').innerHTML += ` <div class="mySlides fade"> <div class="numbertext">${i+1}/${allbook[i].Items.length}</div> <img id="img" src onerror="this.onerror=null; this.src=myFunction(${id});" style="width:100%"> <div class="text" id="title" onclick="myFunction(${id})"></div> </div>`; document.querySelector('#punt').innerHTML += ` <span class="dot" onclick=currentSlide(${i+1})></span>`; myFunction(id); } showSlides(0); } }; outerXmlhttp.open("GET", url, true); outerXmlhttp.send(); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides fade"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } function myFunction(id) { var url1 = "https://wjko8t4509.execute-api.us-east-2.amazonaws.com/books/" + id; innerXmlhttp = new XMLHttpRequest(); innerXmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var myArr = JSON.parse(this.responseText); document.getElementById("img").src = "book_img/" + myArr.Item.immagine; document.getElementById("title").innerHTML = `<a href="fragmentProva.html?id=${id}">${myArr.Item.titolo}</a>`; } }; innerXmlhttp.open("GET", url1, true); innerXmlhttp.send(); }
 <div class="slideshow-container" id="slideshow"> <a class="prev" onclick="plusSlides(-1)">&#10094;</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <div id="punt" style="text-align:center"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM