【问题标题】:Issue with iframe and jqueryiframe 和 jquery 的问题
【发布时间】:2015-12-14 10:39:36
【问题描述】:

我想使用 jquery 解析我的 iframe,例如,我需要在 iframe 文档中搜索字符串“Ctrl”。

我的 iframe 的网址:http://windows.microsoft.com/fr-xf/internet-explorer/ie-keyboard-shortcuts#ie=ie-11

$('#iframeIe').contents()

错误信息:

Uncaught DOMException: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://localhost:4000" from accessing a cross-origin frame.(…)

还有其他方法可以在页面上包含网站并解析它们吗?我的意思是解析他们的文本内容...

【问题讨论】:

  • 是同域的frame还是可以修改cors header?
  • iframe 不在同一个域中,我在本地服务器上...
  • 只有在你同时控制两个域的情况下才能做到这一点

标签: jquery angularjs iframe


【解决方案1】:

我推荐你this thread

你可以试试这个线程的sn-p:

$(function(){//document ready
    $('some selector', frames['nameOfMyIframe'].document).doStuff()
});

如果您有permission denied errors,请查看答案

【讨论】:

    【解决方案2】:

    您可以使用 AJAX:

    $.ajax({
      url: "yourwebsiteurl",
      async: false,
      dataType: 'html',
      cache: false,
      success: function(html){
        $(".div").append(html); // for example append your html to a specific object 
      }
    });
    

    请记住,您只能从同一域内的页面获取数据,否则它会抱怨跨浏览器来源的那种烦人的东西;)。

    【讨论】:

    • 我不在同一个域中,我在本地服务器上。
    • ajax 不能解决跨域问题,它绑定了相同的安全策略
    • 还有其他解析 iframe 的解决方案吗?
    • 我会说看看here@tonymx227
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多