相同域名:如何使用jquery或javascript访问iframe内容

时间:2014-08-05 06:51:30

标签: javascript jquery html css iframe

我在页面中创建了一个动态iframe,并在iframe中加载html内容,但是使用了jquery,无法访问iframe中的html内容。

基于以下网站上的搜索结果,但在我的情况下不适用

  1. iframe拥有与我在src中使用/testhtm.html相同的域名并加载它。
  2. iframe正在加载,因为我在iframe被加载之后发出警报我发起了我的活动
  3. 在控制台上尝试访问iframe时在浏览器中出错:

      

    错误:访问属性'文档'的权限被拒绝
      的document.getElementById( 'testiframe')。contentWindow.document.body.inne ...

    尝试访问iframe内的div:

    $("#testFrame").contents().find("#myDiv");
    

    iframe代码:

    var ifr = $('<iframe/>', {
        id: 'testiframe',
        name: 'genericFrame',
        src: '/testhtml.html?' + serializeForm(params),
        style: 'display: none',
        load: function(){
            alert('iframe loaded !');
        }
    });
    $('#overlay').html(ifr); // Attaching it to overlay so that iframe loads in a lightbox
    

    实际访问div并显示它的任何建议或技巧?

    UPDATE:

    我正在使用javascript获取一些东西:

    var ifrBody = document.getElementById('testiframe').contentWindow;
    
    returns me window object with content but i am unable to access it
    

    但是使用jquery会抛出错误:

     var iBody = $("#testFrame").contents().find("body");
    
    Error: Permission denied to access property 'document'
    

0 个答案:

没有答案