首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-02-05 
怎样获取Iframe中的内容TestIframe.htmlhtmlheadscript typetext/javascriptalert(document.getE

怎样获取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……

热点排行