jQuery 从 iframe 获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16589306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:25:27  来源:igfitidea点击:

Get data from iframe

javascriptjqueryhtml

提问by deck john

I am doing this first time. I have created an iframeon my page and I want the text from the iframethrough jquery. Here is my code :

我是第一次这样做。我已经创建了一个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>

text.php:

文本.php:

text to change

I tried a lot in all browsers but still this is not working. Can anyone help me to get this content?

我在所有浏览器中尝试了很多,但仍然无法正常工作。任何人都可以帮助我获取此内容吗?

回答by Petr Vostrel

Use .contents()to get to iFrame's DOM.

使用.contents()访问 iFrame 的 DOM。

$('#myIframe').contents()

UPDATE:

更新:

In the OP:

在操作中:

$("#result").html(iframeContent.find('body').html);

Should say:

应该说:

$("#result").html(iframeContent.find('body').html());

回答by John b

The contentWindow works in both FF and chrome

contentWindow 适用于 FF 和 chrome

document.getElementById('myFrame').contentWindow.document.body    

Would give you a DOM element body

会给你一个 DOM 元素主体

You can also try something like

你也可以尝试类似的东西

 window.frames['myIframe'].document.body    

That might do the trick for you also

这也可能对你有用

You might have problems with your browsers built in security. If you run this on a local machine. There is a way to disable browsers security.

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

回答by Higarian

Doing with jquery will be a little easier:

使用 jquery 会更容易一些:

$('Your Selector', frames['myIframe'].document)

The above example will get anything from myIframe. But the iframe MUST be from the same domain as the parent document. If not from the same domain, a security violation occurs (You can't add content from foreign sites to your page and change that content.)

上面的示例将从myIframe获取任何内容。但是 iframe 必须来自与父文档相同的域。如果不是来自同一个域,则会发生安全违规(您不能将来自外国站点的内容添加到您的页面并更改该内容。)

If no security violation, you can do anything with the selection. For example you can use the jquery append() method to insert new html inside the iFrame, you can use the html() method to replace html or any other function that jquery/pure javascript allows.

如果没有违反安全,您可以对选择进行任何操作。例如,您可以使用 jquery append() 方法在 iFrame 中插入新的 html,您可以使用 html() 方法替换 html 或 jquery/pure javascript 允许的任何其他函数。

回答by Sudhakar Singajogi

var content=$("iframe").contents().find('body').html();

alert(content);