<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
//显示iframe中的内容
function list(){
//alert(document.getElementById("ali").contentWindow.document.documentElement.outerHTML);
document.getElementById('content').innerHTML=document.getElementById("ali").contentWindow.document.body.innerHTML;
}
</script>
</head>
<body>
<p><a href="#" onclick="list()">点击</a></p>
<p> </p>
<!--只能显示同域的内容,如果访问www.163.com等不同域名的网站则不行-->
<iframe id="ali" src="f.html" width="1024" height="500"></iframe>
<div id="content"></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="JavaScript" type="text/javascript">
//显示iframe中的内容
function list(){
//alert(document.getElementById("ali").contentWindow.document.documentElement.outerHTML);
document.getElementById('content').innerHTML=document.getElementById("ali").contentWindow.document.body.innerHTML;
}
</script>
</head>
<body>
<p><a href="#" onclick="list()">点击</a></p>
<p> </p>
<!--只能显示同域的内容,如果访问www.163.com等不同域名的网站则不行-->
<iframe id="ali" src="f.html" width="1024" height="500"></iframe>
<div id="content"></div>
</body>
</html>
本文介绍了一个使用JavaScript来读取并显示同一域名下iframe中内容的方法。通过简单的代码示例展示了如何利用JavaScript获取iframe内的HTML内容,并将其显示在当前页面的指定元素中。

4601

被折叠的 条评论
为什么被折叠?



