Javascript 从页面中删除 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12419445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:55:45  来源:igfitidea点击:

Removing iframe from page

javascriptiframe

提问by user1321824

I am creating an iframe dynamically for submmiting a form,after submitting i need to remove the iframe form the page.I removed itas follows but it is not removed,

我正在动态创建一个 iframe 用于提交表单,提交后我需要从页面中删除 iframe 表单。我如下删除了它,但它没有被删除,

function remove(){
 var frame = document.getElementById("upload_iframe"),
 var frameDoc = frame.contentDocument || frame.contentWindow.document;
 frameDoc.removeChild(frameDoc.documentElement);
}

How to remove the ifarme form the form completely.

如何完全删除表单中的 ifarme。

Thanks

谢谢

回答by Sergii Stotskyi

Frame has 2 behaviors: frame as document element (like div or another DOM element) and frame as window element (like global windowobject). So if you want to remove iframe from DOM tree you have to work with iframe like with DOM element

框架有两种行为:框架作为文档元素(如 div 或另一个 DOM 元素)和框架作为窗口元素(如全局window对象)。因此,如果您想从 DOM 树中删除 iframe,您必须像使用 DOM 元素一样使用 iframe

function remove(){
 var frame = document.getElementById("upload_iframe");
 frame.parentNode.removeChild(frame);
}

回答by Mike

A way I've been doing it (since I have a large amount of iframes) is using jQuery,

我一直在做的一种方式(因为我有大量的 iframe)是使用 jQuery,

$('iframe').remove()

or in the case of only one iframe you can remove it using its ID, still with jQuery

或者在只有一个 iframe 的情况下,您可以使用其 ID 删除它,仍然使用 jQuery

$('#iframeID').remove()