为什么我不能得到我的图像出现在表单元格/节点..也许我可以得到一些闭包? [英] Why can't I get my images to appear in table cells/nodes.. maybe I can get some closure?

查看:116
本文介绍了为什么我不能得到我的图像出现在表单元格/节点..也许我可以得到一些闭包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新表格的每个单元格中添加一个新图片,并给它与旧表格相同的来源,然后使其可点击。首先,我这样做:

I want to add a new image in each cell of the new table and give it the same source as the old table, and then make it clickable. Firstly, I did this:

function showData() {
  if (localStorage.getItem(name) !== null) {
    var showme = localStorage.getItem(name);
    alert("I got the table");
    var newTable = document.createElement('table');
    newTable.innerHTML = showme;
    newTable.id = "newTable";

    newNumRows = newTable.getElementsByTagName('tr').length;
    newNumCells = newTable.getElementsByTagName('td').length;
    newNumCols = newNumCells / newNumRows;
    alert(newNumRows);
    alert(newNumCells);
    alert(newNumCols);

    var newImages = newTable.getElementsByTagName('img');
    for (var i = 0; i < newImages.length; i += 1) {
      var picSource = newImages[i]['src'];
      console.log(picSource);
    }

    function addNewImage(newNumCols) {
      var newImg = new Image();
      newImg.src = picSource;
      col.appendChild(newImg);
      newImg.onclick = function() {
        alert("WOW");
      };
    }

    for (r = 0; r < newNumRows; r++) {
      row = newTable.insertRow(-1);

      for (c = 0; c < newNumCols; c++) {
        col = row.insertCell(-1);
        addNewImage(newNumCols);
      }
    }

    var showIt = document.getElementById('holdTable');
    showIt.appendChild(newTable);
  }
}

这在一定程度上起作用,仅显示最后一个图像。所以,我做了一些环顾四周,我认为它有关闭(对任何重复的歉意),但它是一个概念,我真的很难理解。所以我试着这样:

This works to a certain extent, but, unfortunately, only the last image was displaying. So, I did a bit of looking around and I think it has to do with closure (apologies for any duplication), but it's a concept I am really struggling to understand. So then I tried this:

function showData() {
  if (localStorage.getItem(name) !== null) {
    hideTaskForm();
    var showme = localStorage.getItem(name);
    var oldTable = document.createElement('table');
    oldTable.innerHTML = showme;
    newTable = document.createElement('table');
    newTable.id = "newTable";
    var i, r, c, j;

    newNumRows = oldTable.getElementsByTagName('tr').length;
    newNumCells = oldTable.getElementsByTagName('td').length;
    newNumCols = newNumCells / newNumRows;
    var newTableCells = newTable.getElementsByTagName('td');
    var getImages = oldTable.getElementsByTagName('img');

    for (r = 0; r < newNumRows; r++) {
      row = newTable.insertRow(-1);

      for (c = 0; c < newNumCols; c++) {
        makeNodes = row.insertCell(-1);
      }
    }

    for (var j = 0; j < newTableCells.length; j++) {
      var theNodeImage = document.createElement("img");
      newTableCells[j].appendChild(theNodeImage);
      alert(newTableCells[j].innerHTML); //This gives me img tags
    }

    for (i = 0; i < getImages.length; i += 1) {
      var oldSource = getImages[i]['src']; //gets the src of the images from the saved table
      console.log(oldSource);
      //alert(oldSource);//successfully alerts the image paths
      var newPic = new Image(); //creates a new image

      (function(newPic, oldSource) {
        newPic.src = oldSource;
        alert(newPic.src); //gives the same image paths
        newTable.getElementsByTagName('img').src = newPic.src; //This doesn't work - table is blank???
      })(newPic, oldSource);
    }

    var showIt = document.getElementById('holdTable');
    showIt.appendChild(newTable);
  }
}

现在,这不会引发任何错误。但是,它也不填充表。它确实给我源,我想我已经创建了新的图像对象附加到newTableCells中的img标签,但表显示为空白。我不知道我在哪里错了。所有的帮助真的欢迎。

Now, this doesn't throw any errors. However, nor does it fill the table. It does give me the source and I think I have created the new image objects to attach to the img tags in the newTableCells, but the table is showing up blank. I don't know where I am going wrong. All help really welcome.

注意:即使作为一个爱好者,即使我知道有很多更有效的方法来做到这一点,但我故意这样做

Note: Even as a hobbyist, even I know there are probably tons of more efficient ways to do this, but I purposely did it this way to try and help me understand the logic of each step I was taking.

推荐答案

在你的代码中,你有:

var newImages = newTable.getElementsByTagName('img');

for (var i = 0; i < newImages.length; i += 1) {
  var picSource = newImages[i]['src'];
  console.log(picSource);
}

最后, picSource 最后一个图片的 src 属性值。然后有:

At the end of this, picSource has the value of the last image's src attribute. Then there is:

function addNewImage(newNumCols) {
  var newImg = new Image();
  newImg.src = picSource;
  col.appendChild(newImg);
  newImg.onclick = function() {
    alert("WOW");
  };
}

值传递到 newNumCols 在函数中。 picSource 的值来自外部执行上下文,并且未更改,因此它仍然是上一个for循环的最后一个图像src。

A value is passed to newNumCols but not used in the function. The value of picSource comes from the outer execution context and is not changed, so it's still the last image src from the previous for loop.

for (r = 0; r < newNumRows; r++) {
  row = newTable.insertRow(-1);

  for (c = 0; c < newNumCols; c++) {
    col = row.insertCell(-1);
    addNewImage(newNumCols);
  }
}



此回路只是继续调用 addNewImage

This loop just keeps calling addNewImage with a single parameter that isn't used in the function, so you get the same image over and over.

对于记录, addNewImage

For the record, the addNewImage function does have a closure to picSource, but it also has a closure to all the variables of the outer execution contexts. This isn't the issue, though it perhaps masks the fact that you aren't setting a value for picSource on each call, so you get the left over value from the previous section of code.

您尚未提供任何有关 showme 内容的指示,因此无法确定此方法是否可以使用。

You haven't provided any indication of the content of showme, so it's impossible to determine if this approach will work at all.

您在哪里:

var showme = localStorage.getItem(name);
alert("I got the table");
var newTable = document.createElement('table');
newTable.innerHTML = showme;
newTable.id = "newTable";

IE不支持设置表格元素的innerHTML属性, innerHTML,并设置单元格的innerHTML(tr,th)。如果您想使用此方法,请考虑:

IE does not support setting the innerHTML property of table elements, though you can create an entire table as the innerHTML of some other element and set the innerHTML of a cell (tr, th). If you want to use this approach, consider:

var div = document.createElement('div');
div.innerHTML = '<table id="newTable">' + showme + '<\/table>';
var newTable = div.firstChild;

这篇关于为什么我不能得到我的图像出现在表单元格/节点..也许我可以得到一些闭包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆