a.htm:
<!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=utf-8" />
<title>a.htm</title>
<script type="text/javascript">
var xmlHttp=null;
//判断浏览器
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
//Firefox,Netscape,Chrome,Safari等浏览器
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { //IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //创建xmlHttp对象
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //创建xmlHttp对象
} catch (e) { }
}
}
}
function startRequest()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange=onReadyState;
xmlHttp.open("GET","myself.htm",true);
xmlHttp.send(null); //发送请求
}
function onReadyState()
{
if(xmlHttp.readyState==4 && xmlHttp.status==200)
alert(xmlHttp.responseText);
}
//窗体加载事件
window.onload=function()
{
startRequest();
}
</script>
</head>
<body>
<iframe id="a" height="260px" src="b.htm" scrolling="no"></iframe>
</body>
</html>
b.htm:
<!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=utf-8" />
<title>b.htm</title>
</head>
<body>
<iframe id="b" src="myself.htm" height="200px"></iframe>
</body>
</html>
myself.htm:
<!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-Language" content="zh-cn" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>myself.htm</title>
</head>
<body>
<p>你好,这是myself.htm!</p>
</body>
</html>
结果是:myself.htm中的所有内容.
法二
document.getElementById("MyIFrame").contentDocument.getElementById("s").style.color="blue";
详细代码如下:
TestIFrame.htm:
<html>
<head>
<script type="text/javascript">
function f(){
var doc;
if (document.all){//IE
doc = document.frames["MyIFrame"].document;
}else{//Firefox
doc = document.getElementById("MyIFrame").contentDocument;
}
doc.getElementById("s").style.color="blue";
}
</script>
</head>
<body onload="f()">
<iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.htm" width = "100" height="100">
</body>
</html>
MyIFrame.htm:
<h1 id = "s" style="color:red;" >内容<h1>
JavaScript获取iframe里面的src路径里面的内容
最新推荐文章于 2025-07-22 11:13:40 发布