[英]jQuery: How to manipulate the contents of an iframe when the src is a different domain?
I have an iframe loading a page, and I would like to manipulate the contents. 我有一个加载页面的iframe,并且我想操纵其中的内容。
For example: 例如:
<iframe id="myIframe" src="http://www.google.com"></iframe>
<script>
$(document).ready(function(){
$(iframeContents).css('background-color', '#000');
});
</script>
How can I do this? 我怎样才能做到这一点?
You can't. 你不能。 The Same Origin Policy forbids this. 同源起源策略禁止这样做。
As Pekka said, you can't, not directly. 正如Pekka所说,不能,不能直接。 The JavaScript security model prohibits this. JavaScript安全模型禁止这样做。 However, you can do it if the domain in the iFrame is the same as the one in which the script runs. 但是,如果iFrame中的域与脚本运行所在的域相同,则可以执行此操作。 So what can be done is to set up a proxy on your own domain, and show the content in the iFrame through the proxy and then use JS to interact with it. 因此,可以做的是在自己的域上设置代理,并通过代理在iFrame中显示内容,然后使用JS与之交互。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.