简体   繁体   English

如何通过jQuery删除iframe

[英]How to Remove a iframe by jQuery

I create a iframe in a div by jQuery.Then I load another page(lets say page A.jsp) into that iframe. 我在jQuery的div创建了一个iframe ,然后将另一个页面(比如说A.jsp页面)加载到该iframe中。 The page 'A.jsp' has some functionality and end of it, the parent iframe should be removed from the first page when a button in 'A.jsp' is pressed . 页面“ A.jsp”具有某些功能,并且在其末尾,应在按下“ A.jsp”中的按钮时从第一页上移除父iframe。 Loading the page 'A.jsp' into the iframe is OK. 可以将页面“ A.jsp”加载到iframe中。 but I can't remove the parent iframe. 但我无法删除父iframe。 How I tried was, select the parent tag iframe then, remove it. 我尝试过的方法是,选择父标记iframe然后将其删除。 but it didn't work. 但这没用。 Here is how I tried.Here $(this) referes to a button in page A.jsp and &('div iframe')referes to the parent iframe 这是我尝试的方法。这里$(this)指的是A.jsp页面中的按钮,&('div iframe')指的是父iframe

$(this).closest($('div iframe')) .animate({
                                   opacity:0
                               },500,function(){$(this).remove()})

How can I remove the parent iframe ? 如何删除父iframe?

This should give you the info you need. 这应该为您提供所需的信息。

https://stackoverflow.com/a/4689154/897871 https://stackoverflow.com/a/4689154/897871

Note: the page that hosts the iframe and the page with the content will have to reside on the same host. 注意:托管iframe的页面和包含内容的页面必须位于同一主机上。

The iframe can be fully remove from the DOM by the remove method eg 可以通过remove方法将iframe从DOM中完全删除,例如

This is my iframe 这是我的iframe

<div id="videoPlayerOP" > <iframe id="optionsPlayVideo" width="520" height="348" style="margin-left: 80px;margin-top: 7px;" src="about:blank"></iframe></div>

After loading some source in it, It can be remove by 在其中加载一些源之后,可以通过以下方式将其删除

$($("#optionsPlayVideo").parent()).empty();

If you want to reuse them them append another iframe in it 如果您想重复使用它们,请在其中附加另一个iframe

$('#videoPlayerOP').append('<iframe id="optionsPlayVideo" width="520" height="348" style="margin-left: 80px;margin-top: 7px;" src="about:blank"></iframe>');

Thanks 谢谢

To which element does this refer to? this指的是哪个元素?

To access the iframe you should point to: 要访问iframe,您应该指向:

parent.document

or 要么

parent.$("iframe")

if you want to use jQuery. 如果您想使用jQuery。 Same Origin Policy is always involved here, so pay attention to what you can actually do. 此处始终涉及“同源政策”,因此请注意实际操作。

Another solution is to reload the parent page using a simple link that has target="_parent" . 另一种解决方案是使用具有target="_parent"的简单链接重新加载父页面。

see also here: 另请参阅此处:

How to access parent Iframe from javascript 如何从JavaScript访问父iframe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM