返回具有localstorage数据的单击功能

时间:2013-07-09 22:21:12

标签: javascript local-storage

几个月前我一直在研究这个项目,有点忘了它。无论如何,我现在要尝试完成它。我的问题是这个。我有一个动态创建的表,其中每个单元格都是可点击的。在单击时,您可以选择使用与阵列不同的图像填充表格的单元格。这可以。使用localstorage我可以在点击完成后保存表状态以反映所做的更改,但是当我退出页面并重新输入更改的图像时,会重新绘制但是表格不再可点击。我正在努力寻找解决方案。也许你可以帮忙吗?这是一些代码:

    function makeChart() {
    table = document.createElement('table');
    var taskName = document.getElementById('taskname').value,
    header = document.createElement('th'),
    numDays = document.getElementById('days').value, //columns
    howOften = document.getElementById('times').value, //rows
    row,
    r,
    col,
    c;
    target= numDays*howOften;

var cel = null;
var myImages = new Array();
myImages[0] = "../www/images/test.png";
myImages[1] = "../www/images/test.png";
myImages[2] = "../www/images/test.png";
var my_div = document.createElement("div");
my_div.id = "showPics";
document.body.appendChild(my_div);
var newList = document.createElement("ul");
my_div.appendChild(newList);

if (taskName == '' || numDays == '') {
    alert('Please enter task name and number of days');
}
if (howOften == '') {
    howOften = 1;
}
if (taskName != '' && numDays != '') {


    for (var i = 0; i < myImages.length; i++) {
        var allImages = new Image();
        allImages.src = myImages[i];
        allImages.onclick = function (e) {
            if (sel !== null) {
                sel.src = e.target.src;
                my_div.style.display = 'none';
                sel.onclick = null;
                sel = null;
            }
        };

        var li = document.createElement('li');
        li.appendChild(allImages);
        newList.appendChild(li);
    }
    my_div.style.display = 'none';

    header.innerHTML = taskName;
    table.appendChild(header);

    function addImage(col) {
        var img = new Image();
        img.src = "../www/images/imageholder.png";
        col.appendChild(img);
        img.onclick = function () {
            my_div.style.display = 'block';
            sel = img;
        };
    }
    for (r = 0; r < howOften; r++) {
        row = table.insertRow(-1);
        for (c = 0; c < numDays; c++) {
            col = row.insertCell(-1);
            addImage(col);
        }
    }
    document.getElementById('singleUser').appendChild(table);
    var blankVal = {
            value : ''
          };
          $("#taskname").attr(blankVal);
          $("#days").attr(blankVal);
          $("#times").attr(blankVal);

    document.getElementById('createChart').onclick = null;
    saveData();
    }
    }
function saveData(){
    localStorage.setItem(name, table.innerHTML);
}
function showData(){
var showme = localStorage.getItem(name); 
var newTable = document.createElement('table');
newTable.innerHTML= showme;
newTable.id = "newTable";
var showIt= document.getElementById('holdTable');
showIt.appendChild(newTable);
console.log(showIt); 
};

0 个答案:

没有答案