如何使用 javascript 替换或删除这些元素

发布于 2024-12-16 13:59:54 字数 560 浏览 5 评论 0原文

我已经创建了下面的函数来创建视频元素的负载,但是如何让 javascript 再次删除它或在加载这些元素后替换元素?我还有另外两个类似的函数,它们只是加载不同的视频。我希望当调用其他两个函数之一时这些元素消失,以便可以看到新的视频元素。

function createVideo1(){
//alert("createSmallVideo has been called");  
var video = document.createElement("video");
video.setAttribute("id", "VideoElement");
video.setAttribute("src", "videos/surfing with friends.ogv");
video.setAttribute("controls", ""); 
video.setAttribute("width", "480");
video.setAttribute("height", "300");
document.getElementById("VideoContainer").appendChild(video);
video.play();
}

I've created the function below to create a load of video elements but how can I get javascript to delete it again or replace elements after these have been loaded? I've got another two functions like this that just load different videos. I want these elements to dissapear when either one of the other two function is called, so the new video elements can be seen.

function createVideo1(){
//alert("createSmallVideo has been called");  
var video = document.createElement("video");
video.setAttribute("id", "VideoElement");
video.setAttribute("src", "videos/surfing with friends.ogv");
video.setAttribute("controls", ""); 
video.setAttribute("width", "480");
video.setAttribute("height", "300");
document.getElementById("VideoContainer").appendChild(video);
video.play();
}

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

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

发布评论

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

评论(2

千纸鹤 2024-12-23 13:59:54

您可以删除使用此功能创建的当前视频元素。只需在创建新视频元素并插入之前调用它即可。

function removeVideo() {
    var video = document.getElementById("videoElement");
    video.parentNode.removeChild(video);
}

You can remove the current video element that you created with this function. Just call it before you create a new video element and insert it.

function removeVideo() {
    var video = document.getElementById("videoElement");
    video.parentNode.removeChild(video);
}
勿挽旧人 2024-12-23 13:59:54

替换

document.getElementById("VideoContainer").appendChild(video);

通过

var container = document.getElementById("VideoContainer");
container.replaceChild(video, container.firstElementChild);

这种方式,您只需将已有的视频元素替换为新的视频元素即可。

Replace

document.getElementById("VideoContainer").appendChild(video);

With

var container = document.getElementById("VideoContainer");
container.replaceChild(video, container.firstElementChild);

This way your simply replacing the video element that's already there with a new video element.

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