怎样获取Iframe中的内容
TestIframe.html
<html>
<head>
<script type="text/javascript">
alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
function f(){
var doc;
if (document.all){//IE
doc = document.frames["MyIFrame"].document;
}else{//Firefox
doc = document.getElementById("MyIFrame").contentDocument;
}
doc.getElementById("s").innerHTML += "123456789";
}
</script>
</head>
<body onload="f()">
<iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500">
</body>
</html>
MyIFrame.html
<h1 id = "s" style="color:red;" >内容<h1>
firefox 提示document.getElementById("MyIFrame") is null
------解决方案--------------------执行
alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
的时候,iframe还不存在
你把这段script放在
<iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500">
的下面就行了
------解决方案--------------------window.top
window.left
window.right
window.parent
通过他们
------解决方案--------------------