如何使用 JavaScript 删除 iFrame 中的 HTML 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31808185/
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:13:17  来源:igfitidea点击:

How to delete HTML Elements in an iFrame using JavaScript

javascriptjqueryhtmlcssiframe

提问by adrinho

I have an iFrame and a div with content in it. I want to delete the div via JavaScript, is that possible and how could I do that?

我有一个 iFrame 和一个包含内容的 div。我想通过 JavaScript 删除 div,这可能吗,我该怎么做?

I don't want to just not display it (eg. display: nonevia CSS) but remove it from the HTML of the site. I have basic knowledge of JavaScript but don't have any experience working with an iFrame.

我不想只是不显示它(例如,display: none通过 CSS)而是从网站的 HTML 中删除它。我有 JavaScript 的基本知识,但没有任何使用 iFrame 的经验。

回答by sahil gupta

You can use

您可以使用

$("#iFrameId").contents().find("#yourDiv").empty();

It is better to use remove()

最好使用 remove()

example: $("#iFrameId").contents().find("#yourDiv").remove();

例子: $("#iFrameId").contents().find("#yourDiv").remove();

Explaination

说明

empty()will remove all the contents of the selection.

empty()将删除选择的所有内容。

remove()will remove the selection and its contents and all the event handlers associated with it.

remove()将删除选择及其内容以及与其关联的所有事件处理程序。

For reference: 1) http://api.jquery.com/remove/2) http://api.jquery.com/empty/

供参考:1) http://api.jquery.com/remove/2) http://api.jquery.com/empty/

回答by Vineet Tyagi

You can try something like:-

您可以尝试以下操作:-

frame.removeChild(//pass div id here);