一、文件1:test.html

<!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>读取iframe源码</title>
</head>

<body>
<p> <span &nbsp;&nbsp; <span &nbsp;&nbsp; <span &nbsp;&nbsp; <span </p>
<iframe id="url" src="none.html" frameborder="0" scrolling="No"></iframe>
<br />
<textarea name="textarea" cols="100" rows="20" id="code"></textarea>
</body>
<script type="text/javascript">
function go1()
{
document.getElementById("code").value="";//清空
document.getElementById("code").value=document.documentElement.innerHTML;//当前页全部
}

function go2()
{
//document.getElementById("code").value=document.body.innerHTML;
document.getElementById("code").value="";
document.getElementById("code").value=document.getElementsByTagName("body")[0].innerHTML;//当前页body
}

function go3()
{
var ifm = document.getElementById("url");
var subWeb = ifm.contentDocument;

document.getElementById("code").value=subWeb.documentElement.innerHTML;//iframe页全部
}

function go4()
{
var ifm = document.getElementById("url");
var subWeb = ifm.contentDocument;

document.getElementById("code").value=subWeb.body.innerHTML;//iframe页body
//document.getElementById("code").value=subWeb.getElementsByTagName("body")[0].innerHTML;
}
</script>
</html>

二、文件2:none.html

<!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>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head>

<body>
<span class="STYLE1">123</span>
</body>
</html>

 

注:

1、document.body等于document.getElementsByTagName("body")[0]

2、innerHTML也可以换成textContent(firefox、ie9)或者innerText(IE)

3、javascript textContent与innerText的异同分析