[英]javascript getItem prints last array when i want to print all items in array
function loadImg () {
for(let y=1;y<=nuotraukos.length;y++)
{
const myImg = document.createElement("img");
myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]);
console.log(localStorage.getItem('nuotraukos',[y]));
myImg.alt=[y]+'- img';
myImg.width='250';
myImg.height='200';
const myDiv = document.querySelector(".imagePlace");
myDiv.appendChild(myImg);
}
}
so with code myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]);
所以代码
myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]);
i want to print all arrays items that are inside, but when i look at the console.log it only prints last one我想打印里面的所有数组项,但是当我查看 console.log 时,它只打印最后一个
The idea of this 'program' is tu mix images and save when i save i put them in array and after i refresh website i want to show mixed images after pressing button 'show saved' in this case it only shows the last image of mixed images, idk what to do been stuck in this for a while这个“程序”的想法是混合图像并保存,当我保存时我将它们放在数组中,在我刷新网站后我想在按下按钮“显示保存”后显示混合图像在这种情况下它只显示混合的最后一个图像图像,我知道该怎么做被困在这里一段时间
you need to parse the items Array :您需要解析 items Array :
function loadImg () {
const localnuotraukos= JSON.parse(localStorage.getItem('nuotraukos'))
for(let y=1;y<=nuotraukos.length;y++)
{
const myImg = document.createElement("img");
myImg.src = 'images/'+localnuotraukos[y] ;
console.log(localnuotraukos[y]);
myImg.alt=[y]+'- img';
myImg.width='250';
myImg.height='200';
const myDiv = document.querySelector(".imagePlace");
myDiv.appendChild(myImg);
}
As you said you are using this code to save in local storage正如您所说,您正在使用此代码保存在本地存储中
for(var i=0;i<=7;i++)
{
let removeImg = document.querySelector('img');
removeImg.remove();
const myImg = document.createElement("img");
myImg.src = 'images/'+nuotraukos[i];
myImg.alt=[i]+'- img';
myImg.width='250';
myImg.height='200';
const myDiv = document.querySelector(".imagePlace");
myDiv.appendChild(myImg);
localStorage.setItem('nuotraukos',nuotraukos[i]); // ********
}
The line which I refer to with ********
is saying that you are override the nuotraukos
item and it will be the last element in the array.我用
********
引用的那一行是说你覆盖了nuotraukos
项目,它将是数组中的最后一个元素。
So, you need to save the array first after initiating your images array因此,您需要在启动图像数组后先保存数组
localStorage.setItem('nuotraukos', JSON.stringify(nuotraukos)); // save all array elements in one item
And when you need to load the array you have to load it and parse to JSON then iterate in the for loop.当您需要加载数组时,您必须加载它并解析为 JSON,然后在 for 循环中进行迭代。 And note that
getItem()
method takes only one parameter which is the key.并注意
getItem()
方法只接受一个作为关键的参数。
function loadImg () {
let imgArray = JSON.parse(localStorage.getItem('nuotraukos')); // get array and parse it
for(let y=0; y <imgArray.length; y++) // iterate from 0 to length
{
const myImg = document.createElement("img");
myImg.src = 'images/'+ imgArray[y];
myImg.alt= y +'- img';
myImg.width='250';
myImg.height='200';
const myDiv = document.querySelector(".imagePlace");
myDiv.appendChild(myImg);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.