创建图像并将其附加到 div 中。怎么了?

发布于 2025-01-06 01:52:53 字数 484 浏览 0 评论 0 原文

所以我想做的就是收集页面上的图像并将它们添加到弹出的灯箱中。所以这就是我正在做的事情......

var images = document.getElementsByTagName('img');
for(var i=0;i<images.length;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}

但是

div.appendChild(img);

不起作用并且它破坏了一切。我可以将其注释掉,而我拥有的其他所有内容(例如,弹出灯箱)都工作正常,所以我知道该行有问题,但我不确定它有什么问题。所以我的问题是我这条线做错了什么?

So what I want to do is gather images on the page and add them to a lightbox that pops up. So here is what I'm doing...

var images = document.getElementsByTagName('img');
for(var i=0;i<images.length;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}

but

div.appendChild(img);

is not working and it breaks everything. I can comment it out and everything else I have (for instance, pop up the lightbox) works fine, so I know there is something wrong with that line but I'm not sure what is wrong with it. So my question is what am I doing wrong with that line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-01-13 01:52:53

问题是 getElementsByTagName()< /code> 返回 实时 NodeList它将自动添加与选择器匹配的任何新元素(在本例中为任何 img 元素)到它的集合。

因此,您将进入无限循环(因为 images 的大小随着每次迭代而增加 1)。解决此问题的最简单方法(有一个巨大的警告)是简单地缓存之前之前的images集合的大小> 你开始添加更多;

var images = document.getElementsByTagName('img');
var len = images.length;
for(var i=0;i<len;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}
// to see what happens, try alerting images.length now to see the length has changed.

但是,这假设新添加的元素将添加到 NodeList末尾;然而,这只是当添加的元素是 DOM 中的最后一个元素时的情况
(通常不会是这种情况/无法保证)。

因此,您最终必须将返回的 NodeList 复制到数组中,然后对其进行迭代;

// Create an array
var images = [];

// Copy the result of getElementsByTagName in to the array
for (var i=0, ar = document.getElementsByTagName('img');i<ar.length;i++) {
    images.push(ar[i]);
}

for(var i=0;i<images.length;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}

可以在 MDC 网站

The problem is that getElementsByTagName() returns a live NodeList, which will automatically add any new elements which match the selector (in this case any img element) to it's collection.

Because of this, you're getting into an infinite loop (as the size of images is growing by 1 with each iteration). The easiest way to fix this (with one massive caveat) is to simply cache the size of the images collection before you start adding more;

var images = document.getElementsByTagName('img');
var len = images.length;
for(var i=0;i<len;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}
// to see what happens, try alerting images.length now to see the length has changed.

However, this assumes that the newly added elements will get added to the end of the NodeList; however this is only the case when the element added is the last in the DOM
(which will usually not be the case/ cannot be guaranteed).

Because of this, you end up with having to copy the NodeList returned into an array, and then iterating over that;

// Create an array
var images = [];

// Copy the result of getElementsByTagName in to the array
for (var i=0, ar = document.getElementsByTagName('img');i<ar.length;i++) {
    images.push(ar[i]);
}

for(var i=0;i<images.length;i++){
      var div = document.getElementById('thediv');
      var img = document.createElement('img');
      img.src = images[i].src;
      div.appendChild(img);
}

A less-dry (compared to the W3 spec!) article that explains this in more detail can be found on the MDC website.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文