从iFrame卸载/删除内容

从iFrame卸载/删除内容

问题描述:

有没有要卸载一个加载在iframe内的页面?如果可能,我不想将iframe src更改为空白页。我基本上正在寻找一些这样的东西,这样的东西就像这样 $('#frameID')。attr(src,); ,除了代码似乎不清除以前加载的页面。

Is there anyway to unload a page that has been loaded inside an iframe? I do not want to change the iframe src to a blank page if possible. I am basically looking for something that will do something like this $('#frameID').attr("src",""); except that code does not seem to clear the previously loaded page.

是否有一个卸载函数,我可以调用哪个将重置iframe所以它没有加载任何内容?

Is there a "unload" function that I can call which will reset the iframe so that it does not have any content loaded inside?

其他解决方案使用 innerHTML ,这在XHTML中并不总是有效。他们也只清除 document.body < head> 中的任何内容仍然存在)。这是一个使用DOM的解决方案:

The other solutions use innerHTML, which won't always work in XHTML. They also only clear document.body (anything in the <head> is still present). Here is a solution that uses the DOM:

var frame = document.getElementById("myFrame"),
frameDoc = frame.contentDocument || frame.contentWindow.document;
frameDoc.removeChild(frameDoc.documentElement);

此解决方案使用 innerHTML

var frame = document.getElementById("myFrame"),
frameDoc = frame.contentDocument || frame.contentWindow.document;
frameDoc.documentElement.innerHTML = "";