iframe内容存储在textarea中

时间:2014-12-10 09:24:32

标签: javascript html css

有没有办法从iframe中检索内容并使用纯JavaScript将其存储在textarea中? 我已使用以下代码退出iframe中的内容:

function getIframeContent(){
 var frameObj = document.getElementById(frameId);
 var frameContent = frameObj.contentWindow.document.body.innerHTML;
 alert("frame content : "+frameContent);
 }

现在如何将它存储在textarea ??

1 个答案:

答案 0 :(得分:0)

您可以在TextArea内显示iFrame的内容,如此

 document.getElementById('YourTextArea').innerHTML = frameContent;

或使用Value属性:

document.getElementById('YourTextArea').value = frameContent;

我使用第一种方法为你做了quick demo

这将返回一个包含任何html标记的字符串。因此,例如,如果您的iframe包含列表,则该方法返回一个html列表。

以下是代码:

<body>
    <div id="frameId">
      <ul>
        <li id="item1">Coffee</li>
        <li id="item2">Tea</li>
      </ul>
    </div>

    <p>Click the button get the HTML content of the iframe.</p>

    <textarea id="demo"></textarea><br/>

    <button onclick="myFunction()">Try it</button>

  <script>
    function myFunction() {
        var frameContent = document.getElementById("frameId").innerHTML;
        document.getElementById("demo").innerHTML = frameContent;
    }
  </script>
</body>