首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单击Javascript按钮时更新数组

在单击Javascript按钮时更新数组
EN

Stack Overflow用户
提问于 2014-03-12 19:51:29
回答 1查看 1.2K关注 0票数 0

这个问题可能有点冗长,但请耐心听我说。

每次用户单击保存按钮时,我都会尝试更新和数组。

当他们点击保存时,页面上的画布图像就被创建了。

这些DataURI值保存在一个数组中。

一旦保存了值,就会创建一个排序的缩略图,并将其添加到屏幕底部。

单击这些图像上的X图标将调用一个函数,以从数组中删除正确的图像。

然后应使用更新后的数组值重新绘制图像,从而将其从屏幕上删除。

我已经包含了一些图片来尝试和演示:

图像#1 (单击保存并在下面添加图像时):

http://postimg.org/image/cybazwydf/

图像#2 (关闭屏幕图像后,添加新图像将删除的图像与新图像一起再次添加):

http://postimg.org/image/gi5pcornl/

这就是问题所在,它会重新添加删除的值。

我将在下面发布它的代码:

代码语言:javascript
复制
function getDataUrl () {
var a = document.getElementById("theCanvas");
var context = a.getContext("2d");
var dataURL = a.toDataURL();
save(dataURL);
}


var theImages = new Array();




//Add dataURL to array:
function save(URL) {
    theImages.push(URL);
    var x = JSON.stringify(theImages);
    localStorage.setItem('images', x);


drawImages(x);
}

function drawImages(array){

    var array = localStorage.getItem("images");
    array = JSON.parse(array);

    //If an image is saved, display the saveArea div:
    if (array.length > 0){
        document.getElementById("saveArea").style.visibility="visible";
    }

    //Clear the elements that might already be in the div so they don't appear twice:
    var theDiv = document.getElementById("saveArea");
    while (theDiv.firstChild) {
    theDiv.removeChild(theDiv.firstChild);
    }


    for (var x=0; x < array.length; x++){ 
                    //Create image for each value in array:



                    var divimg = document.createElement("div");

                    divimg.style.marginRight="10px";
                    //divimg.style.border = "1px dotted red";
                    divimg.className = "saveContainer";
                    divimg.style.width = 300+"px";
                    divimg.style.padding = 5+"px";
                    divimg.style.marginRight="10px";
                    divimg.style.height = 150+"px";
                    divimg.style.display="inline-block";    
                    divimg.style.marginRight="35px";


                    document.getElementById("saveArea").appendChild(divimg);

                    var img = document.createElement("img");
                    img.src = array[x];
                    img.width = 300;
                    img.height = 150;
                    img.setAttribute("id", "theImageId");
                    img.style.marginRight="10px";
                    img.className = "saveImg";


                    //Add each image to the containing div:
                    divimg.appendChild(img);




                    //Create close button: 
                    var close = document.createElement("img");
                    close.src="close.png";
                    close.width = 50;
                    close.height = 50;
                    close.border = 0;
                    close.style.position="relative";
                    close.style.bottom=115+"px";
                    close.style.right=40+"px";
                    close.className="closeButton";
                    //close.style.cssFloat="right";
                    //close.style.right= 0+"px";


                    var link = document.createElement("a");
                    link.href = "#";

                    link.appendChild(close);

                    link.nameIndex = x;

                    //WHEN THE USER CLICKS THE CLOSE ICON:
                    link.onclick = (function (x) {
                    var imageNum = this.nameIndex;
                    alert("You clicked to close image "+(imageNum+1));
                        //Remove the image:
                        array.splice(x,1);

                        alert("The length of this array is: "+array.length);
                        //Update localStorage:
                        localStorage.removeItem('images');
                        array = JSON.stringify(array);

                        localStorage.setItem('images', array);
                        drawImages(array);
                    } );




                    //Add the close button the the containing div:
                    divimg.appendChild(link);
                    //divimg.appendChild(close);

    } //End Loop

} //End drawImages();

我已经试着解决这个问题好几个小时了,但是没有成功..

EN

回答 1

Stack Overflow用户

发布于 2014-03-12 20:10:38

从数组中删除图像后,您不会将其存储在任何位置,因此拼接结果会丢失,而数组保持不变

代码语言:javascript
复制
    array.splice(x,1);

需要的是

代码语言:javascript
复制
    array =  array.splice(x,1);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22350632

复制
相关文章

相似问题

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