【问题标题】:NotFoundError Node was not found image_div.parentNode.removeChild(img)NotFoundError 节点未找到 image_div.parentNode.removeChild(img)
【发布时间】:2014-12-19 17:37:47
【问题描述】:

我在使 removeChild() 工作时遇到问题。我收到“NotFoundError:找不到节点 image_div.parentNode.removeChild(img);”错误

这是我的代码:

<div id="imagesframe"> </div>

<script>
images_array = [image1.jpg,image2.jpg,image3.jpg,image4.jpg,image5.jpg];
var image_div = document.getElementById('imagesframe');
for(var i=0 ; i<images_array.length ; i++) {
    var img = new Image();
    img.src = images_array[i];
    img.style.width = '500px';
    img.style.height = '500px';
    setTimeout(function(){image_div.appendChild(img)},1000);
    image_div.parentNode.removeChild(img);
}
</script>

最后一行: image_div.parentNode.removeChild(img); 导致问题。

这可能是什么原因造成的?

【问题讨论】:

  • 为什么要从父级的父级中查找图像? img.parentNode.removeChild(img) 会更好。但是,在超时到期之前无法找到img。也许你误解了setTimeout,它不会延迟其余代码的执行,它只会延迟调用参数中传递的函数。
  • 我什至试过 image_div.removeChild(img);但这不起作用。那么这里有什么用呢?好的,现在我知道我必须修复我的 setTimeout () 函数,这现在更复杂了。看来我得想出另一种解决方案了。
  • 请解释一下您实际上想要做什么?显示一张图片第二张,然后显示下一张等等?
  • 是的,我正在尝试显示一个又一个图像,其中第一个图像消失,下一个图像显示在同一位置,对于循环中的数组中的所有图像。

标签: javascript html


【解决方案1】:

在将正确的img 传递给参数函数时,在循环中使用setTimeout 会使事情变得复杂。此外,您可能已经省略了数组中图像 URL 周围的引号。

然后是setTimeout,它不会阻止循环被执行,它只是一个计时器,它在给定的延迟后启动参数函数。在您的代码中(没有其他错误),所有五个函数调用将在一秒钟后几乎同时完成,因为循环会在微秒内为每一轮创建一个新计时器。

这是一种使用简单外部范围访问的替代方法。

window.onload = function () {
    var imagesArray = ['image1.jpg','image2.jpg','image3.jpg','image4.jpg','image5.jpg'], // URLs of the images
        imageDiv = document.getElementById('imagesframe'), // An element wherein to show the images
        counter = 0, // Image counter
        img;  // A temporary image element

    // Changes images in an element
    function changeImg (first) {
        if (!first) { // Check, if there's an image to remove
            imageDiv.removeChild(img); // Remove an image
        } else { // Reset counter, just in case the slideshow will be reused
            counter = 0;
        }
        // Create a new image and append it to an element
        img = new Image();
        img.src = imagesArray[counter];
        img.style.width = '500px';
        img.style.height = '500px';
        imageDiv.appendChild(img);
        // Show more images or quit
        counter += 1;
        if (counter < 5) { // Show the next image
            setTimeout(changeImg, 1000);
        } else { // Remove the last image
            setTimeout(function () {imageDiv.removeChild(img)}, 1000);
        }
    }
    changeImg(true); // Show the first image
    return;
};

代码放置在head 中,并包含在window.onload 中。如果需要,您可以将其包装在 IIFE 中,并将其放在代码中的原始位置。需要一个包装器来避免声明全局变量。

IIFE = 立即调用函数表达式,看起来有点像这样:

(function () {/* Code to run here */}());

【讨论】:

  • 非常感谢您提供如此详细的代码答案!并没有我最初想的那么简单!递归在这里绝对有意义。
  • 实际上代码不是递归的,它是从外部重新调用的。无论如何,这种类型的实现被广泛用于超时。您也可以使用循环来执行此操作,但它会比我的简单方法更复杂。如果您觉得答案有用,您可以通过勾选答案左上角的复选标记来接受它。通过接受答案,其他访问者可以看到您的问题已解决,我们都将获得一些代表
  • 不是递归的? From line if (counter
  • changeImg 不在changeImg 的内部调用,并且执行也不会返回到changeImg,因此代码不是递归的。定时changeImages 由浏览器以与事件监听非常相似的过程调用(除非相同),并且执行也返回到该过程。关于投票,您需要 15 个代表才能获得投票特权。很快就赚到了,如果你觉得它解决了你的问题,你可以从接受答案开始; )。
  • 我接受了你的回答 :-) 我想我对你的意思感到困惑,这不是递归的。因为我从您的代码答案中看到了内部的函数调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多