【问题标题】:Access iframe content from parent jQuery从父 jQuery 访问 iframe 内容
【发布时间】:2013-06-17 07:21:40
【问题描述】:

我想访问父 jQuery 上的 iframe 内容。

我在父页面上添加了以下代码。

$(document).ready(function () {
    $('#MyIframe').load(function () {
        $('#MyIframe').contents().find('body').html('Hey, i`ve changed content of <body>! Yay!!!');
    });
});

我的 iframe 页面从其他网站加载它给我一个错误

Error: Permission denied to access property 'ownerDocument'

[Break On This Error]   

...f ( ( context ? context.ownerDocument || context : preferredDoc ) !== document )...

【问题讨论】:

  • 而且 iFrame 当然是显示来自同一个域的内容?

标签: javascript jquery asp.net iframe


【解决方案1】:

如果您删除 iframe 的 onload,代码将正常工作。 http://jsfiddle.net/qPFza/

$(document).ready(function () {
    $('#myiframe').contents().find('body').html('Hey, i`ve changed content of <body>! Yay!!!');
});

所以你的问题是因为你的 iframe 的内容没有被你设置的 onload 触发。

看这个例子,这里也没有调用加载函数。 http://jsfiddle.net/qPFza/1/

现在这个例子中的内容来自不同的网站,不是让你改变它。 http://jsfiddle.net/qPFza/2/

如果网页和 iframe 来自不同的网站,则不能更改其中的一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2011-03-26
    相关资源
    最近更新 更多