我想使用jquery在iframe中操纵html
我认为我可以通过将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()
});
但是,这似乎不起作用.进行一些检查后发现,除非我等待一段时间以加载iframe,否则frame ['nameOfMyIframe']中的变量是未定义的.但是,在iframe加载时,变量不可访问(我收到权限被拒绝的类型错误).
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 way to work around this?
The only "workaround", if you can't make the other site include the relevant CORS headers, would be to fetch the iframe content server side and serve it as coming from your own domain.
之所以没有更简单的解决方法,是因为为什么有这样的相同来源政策:以保护用户.
The reason there isn't simpler workaround is due to why there is this same origin policy : to protect users.
您不能使用跨域iframe进行任何访问