从iframe获取数据

dec*_*ohn 10 html javascript jquery html5

我是第一次这样做.我已经创建了一个iframe我的网页上,我想从文本iframe通过jquery.这是我的代码:

<html>
  <head><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

    <script type="text/javascript">
    function copyIframeContent(iframe){  
        var iframeContent = $(iframe).contents(); //alert(iframeContent);
        //$("#result").text("Hello World");
        $("#result").html(iframeContent.find('body').html);alert(iframeContent.find('body').html());
    }
    </script>
  </head>
  <body>
    <iframe id="myIframe" onload="copyIframeContent(this);" name="myIframe" src="text.php"></iframe><br />
    Result:<br />
    <textarea id='result'></textarea>
    <input type="button" value="click" id="btn" onclick="aa()">
    <script type="text/javascript">
         function aa(){  alert("Fdf");
            alert(document.getElementById('myIframe').contentWindow.document.body.innerHTML);
    }
    </script>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

text.php:

text to change
Run Code Online (Sandbox Code Playgroud)

我在所有浏览器中都尝试了很多,但仍然没有用.谁能帮助我获取这些内容?

Joh*_*n b 4

contentWindow 在 FF 和 chrome 中均适用

document.getElementById('myFrame').contentWindow.document.body    
Run Code Online (Sandbox Code Playgroud)

会给你一个 DOM 元素主体

你也可以尝试类似的东西

 window.frames['myIframe'].document.body    
Run Code Online (Sandbox Code Playgroud)

这也可能对你有用

您的浏览器内置安全性可能存在问题。如果您在本地计算机上运行它。有一种方法可以禁用浏览器安全性。