是否可以使用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)
在 jQuery 中使用 iframe 时使用contents()。
$("#YourIframe").contents().find("html").html();
Run Code Online (Sandbox Code Playgroud)
仅当 iframe与父级位于同一域中时,此方法才有效。