jQuery/JavaScript:访问 iframe 的内容

jQuery/JavaScript:访问 iframe 的内容

问题描述:

我想使用 jQuery 在 iframe 中操作 HTML.

I would like to manipulate the HTML inside an iframe using jQuery.

我想我可以通过将 jQuery 函数的上下文设置为 iframe 的文档来做到这一点,例如:

I thought I'd be able to do this by setting the context of the jQuery function to be the document of the iframe, something like:

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

但是这似乎不起作用.一些检查表明 frames['nameOfMyIframe'] 中的变量是 undefined 除非我等待 iframe 加载一段时间.但是,当 iframe 加载时,变量不可访问(我收到 permission denied 类型的错误).

However this doesn't seem to work. A bit of inspection shows me that the variables in frames['nameOfMyIframe'] are undefined unless I wait a while for the iframe to load. However, when the iframe loads the variables are not accessible (I get permission denied-type errors).

有没有人知道解决这个问题的方法?

Does anyone know of a work-around to this?

我认为你正在做的事情取决于 同源策略.这应该是您收到权限被拒绝类型错误的原因.

I think what you are doing is subject to the same origin policy. This should be the reason why you are getting permission denied type errors.