gpt4 book ai didi

javascript - 未捕获的 NotFoundError : Failed to execute 'insertBefore' on 'Node' : The node before which the new node is to be inserted is not a child of this node

转载 作者:IT王子 更新时间:2023-10-29 03:23:06 24 4
gpt4 key购买 nike

我在使用 JavaScript 时遇到问题。我收到此错误消息:

Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.

Javascript:

var vidCounter = 0;
vidCounter++;

var originalDiv;
var newVideo = document.createElement("video");

newVideo.setAttribute("name", vidCounter);
newVideo.setAttribute("autoplay", "true");
originalDiv = document.getElementById("othersarea");
document.body.insertBefore(newVideo, originalDiv);

它正在尝试添加 <video>在名为 othersarea 的 div 下方标记.

<div id="remoteVideos">
<div class="title">
<h2>Others</h2>
</div>
<div id="othersarea"></div>
</div>

我该如何解决这个问题?

我也想跑attachMediaStream([VIDEO TAG HERE HOW?], event.stream);在我的新视频标签上。

最佳答案

您必须在之前插入的元素的父元素上调用 insertBeforedocument.body 不是父级,它位于 DOM 层次结构的最上层。

要在 DIV 之后插入,您必须在其下一个同级之前插入。

var parentDiv = document.getElementById("remoteVideos");
parentDiv.insertBefore(newVideo, originalDiv.nextSibling);

请参阅 MDN 中的示例

关于javascript - 未捕获的 NotFoundError : Failed to execute 'insertBefore' on 'Node' : The node before which the new node is to be inserted is not a child of this node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23401641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com