读书人

怎样获取Iframe中的内容,该怎么解决

发布时间: 2012-02-05 12:07:14 作者: rapoo

怎样获取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
通过他们
[解决办法]

探讨
执行
alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
的时候,iframe还不存在
你把这段script放在
<iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" heigh……

读书人网 >JavaScript

热点推荐