使用iframe的内容替换_top窗口的内容而不重新加载页面

Gab*_*res 5 javascript jquery

是否可以使用iframe中加载的文档替换浏览器窗口(_top框架)的内容,而无需发出新的GET来加载该文档?

像这样的东西:

<html>
<head>
<script type="text/javascript" language="JavaScript">
$(document).ready(function() {
  $("#link").click(function() {
    // should present in the browser window the content of the iframe WITHOUT reload
    // even preserve javascript/head code
    $(document).html($("#frameDestination").html());
  });
});
</script>
</head>
<body>
<a id="link" href="#">full window</a>
<iframe id="frameDestination" src="http://www.some-random-site.com" width="900px" height="500px"></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

epa*_*llo 1

在 jQuery 中使用 iframe 时使用contents()。

$("#YourIframe").contents().find("html").html();
Run Code Online (Sandbox Code Playgroud)

当 iframe与父级位于同一域中时,此方法才有效。