documentElement.innerHTML不显示iframe正文 [英] documentElement.innerHTML not showing the iframe body

查看:357
本文介绍了documentElement.innerHTML不显示iframe正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有一个iframe的页面。我通过javascript制作iframe。后来我需要保存该页面并重新打开它的确切内容。例如,如果用户在iframe主体中写了sumthing并保存并重新打开页面,则iframe拥有相同的内容。为了保存页面我的逻辑是获取文档的innerHTML,创建一个新的html文件并将内容写入它。现在prblm是,当我做document.documentElement.innerHTML,iframe部分确实出现在它,但身体部分和HTML部分丢失。唯一出现的是< iframe>< / iframe>
如何获得iframe的HTML以及父页面的HTML?继承人的代码,让你看到什么 document.documentElement.innerHTML 在控制台上打印

 < HTML> 
< head>
< title> iframe test< / title>
< script type =text / javaScript>
函数showStr(){

var customArea = document.getElementById('custom-area');
var newdiv = document.createElement('div');
newdiv.setAttribute('id',myDiv);
customArea.appendChild(newdiv);
var htcontents =< iframe id ='myIframe'frameborder ='1'>< / iframe>;
newdiv.innerHTML = htcontents;
document.getElementById(myIframe)。contentDocument.designMode =on;
}

function showif()
{
console.log(document.documentElement.innerHTML);
}
< / script>
< / head>
< body onLoad =showStr()>
< button id =myButtononClick =showIF()>< / button>
< div id =custom-area>< / div>
< / body>
< / html>

即时通讯在chrome中使用此功能...
谢谢!!

解决方案

基于这篇文章,这里有一个解决方案:

更新

  function showIF()
{
var f = document.getElementById('myIframe');
var d = f.contentDocument? f.contentDocument:f.contentWindow.document;
var customArea = document.getElementById('custom-area');
//读取iframe的内容
var iframeContent = d.body.innerHTML;
//删除iframe自己
f.parentNode.removeChild(f);
//将html附加到父div
customArea.innerHTML + = iframeContent;
//console.log(d.body.innerHTML);
}

至少它适用于我的Chrome浏览器(版本11.0.696.60) p>

i have got a page that has one iframe in it. i am making the iframe via javascript. later i need to save that page and reopen it with the exact contents. like e.g if the user wrote sumthing in the iframe body and saved it and then reopened the page, the iframe shud hav the same contents. to save the page my logic is to get the document's innerHTML, create a new html file and write the contents to it. now the prblm is that when i do document.documentElement.innerHTML , the iframe section does appear in it, but the body section and the html section is missing. the only thing that appears is <iframe></iframe>. how can i get HTML of iframe along with the parent page's HTML? heres the code to let u see what document.documentElement.innerHTML prints on the console

<html>
<head>
   <title>iframe test</title>
<script type="text/javaScript">
function showStr(){

        var customArea = document.getElementById('custom-area');
        var newdiv = document.createElement('div'); 
        newdiv.setAttribute('id',"myDiv");
        customArea.appendChild(newdiv);
        var htcontents = "<iframe id='myIframe' frameborder='1'></iframe>";
        newdiv.innerHTML = htcontents; 
        document.getElementById("myIframe").contentDocument.designMode="on";
}

function showIF()
{
    console.log(document.documentElement.innerHTML);
}
</script>
</head>
<body onLoad="showStr()">
<button id="myButton" onClick="showIF()"></button>
<div id="custom-area"></div>
</body>
</html>

im using this in chrome... thanks!!

解决方案

Based on this post, here a solution:

UPDATE

function showIF()
{
  var f= document.getElementById('myIframe');
  var d= f.contentDocument? f.contentDocument : f.contentWindow.document;
  var customArea = document.getElementById('custom-area');
  //read the content of iframe
  var iframeContent = d.body.innerHTML;
  //delete the iframe himself
  f.parentNode.removeChild(f);
  //Append the html to parent div
  customArea.innerHTML += iframeContent;
  //console.log(d.body.innerHTML);
}

At least it works with my Chrome browser (Version 11.0.696.60)

这篇关于documentElement.innerHTML不显示iframe正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆