How to make an Array of images getting by input field and display the Array image as slider

huangapple go评论59阅读模式
英文:

How to make an Array of images getting by input field and display the Array image as slider

问题

我的问题是如何通过输入字段创建图像数组,并在JavaScript中将数组图像显示为滑块。任何人都可以解决这个问题,请回答我。

请提供JavaScript代码。

document.querySelector("#a").addEventListener("change", function(){
       const reader = new FileReader();
          reader.addEventListener("load", () => {
               localStorage.setItem("recent-image", reader.result)
          });
       reader.readAsDataURL(this.files[0]);
});
document.addEventListener("DOMContentLoaded", () => {
    
    const imageurl = localStorage.getItem("recent-image");

    if(imageurl){
        document.querySelector("#b").setAttribute("src", imageurl);
    }
});

你能否使用数组来完成这个任务,请回答我。

它只能处理一个图像,但我想通过数组在本地存储中存储多个图像,请回答我。

英文:

My problem is to make array of image by input field and display array images as slider in JavaScript
anyone can solve it please answer me

Please give code of JavaScript

    document.querySelector("#a").addEventListener("change", function(){
           const reader = new FileReader();
              reader.addEventListener("load", ()=>{
                   localStorage.setItem("recent-image", reader.result)
              });
           reader.readAsDataURL(this.files[0]);
    });
    document.addEventListener("DOMContentLoaded()", ()=> {
        
        const imageurl = localStorage.getItem("recent-image");

        if(imageurl){
            document.querySelector("#b").setAttribute("src", imageurl);
        }
    });

can you do this in array please answer me

it take only one image image but, I want to store multiple image in local storage by array
please answer me

答案1

得分: 1

以下是翻译好的代码部分:

问题我看到的是要定义您的本地存储并确保图像存储在其中以便稍后遍历每个图像我提供了解决方法的示例

var images = localStorage.getItem('images') || [];

function saveImages() {
    localStorage.setItem('images', JSON.stringify(images));
}

function drawImages() {
    var slider = document.getElementById('slider');
    slider.innerHTML = '';
    for (var i = 0; i < images.length; i++) {
        const img = images[i];
        const html_img = document.createElement('img');
        html_img.src = img;
        html_img.alt = 'Alt img';
        html_img.width = 200;
        html_img.height = 150;
        slider.appendChild(html_img);
    }
}

document.querySelector("#a").addEventListener("change", function(){
    const reader = new FileReader();
    reader.addEventListener("load", ()=>{
        images.push(reader.result);
        drawImages();
    });
    reader.readAsDataURL(this.files[0]);
});

document.addEventListener("DOMContentLoaded", drawImages);
英文:

The problem that I see is to define your localstorage and make sure that the images are stored in it to later go through each one of them, I leave an example of how to solve it

var images = localStorage.getItem(&#39;images&#39;) || [];

function saveImages() {
    localStorage.setItem(&#39;images&#39;, JSON.stringify(images));
}

function drawImages() {
    var slider = document.getElementById(&#39;slider&#39;);
    slider.innerHTML = &#39;&#39;;
    for (var i = 0; i &lt; images.length; i++) {
        const img = images[i];
        const html_img = document.createElement(&#39;img&#39;);
        html_img.src = img;
        html_img.alt = &#39;Alt img&#39;;
        html_img.width = 200;
        html_img.height = 150;
        slider.appendChild(html_img);
    }
}

document.querySelector(&quot;#a&quot;).addEventListener(&quot;change&quot;, function(){
    const reader = new FileReader();
    reader.addEventListener(&quot;load&quot;, ()=&gt;{
        images.push(reader.result);
        drawImages();
    });
    reader.readAsDataURL(this.files[0]);
});

document.addEventListener(&quot;DOMContentLoaded()&quot;, drawImages);

huangapple
  • 本文由 发表于 2023年6月2日 02:28:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定