简体   繁体   English

jQuery:当src是另一个域时,如何处理iframe的内容?

[英]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.

相关问题 从其他域操作iframe - manipulate iframe from a different domain 可以设置iframe的src并随后处理其内容吗? - Can one set an iframe's src and manipulate its contents afterwards? iframe中的内容根本不加载或显示警告 - iframe src是不安全的,而不同的域是包含页面 - Contents inside an iframe don't load at all or a warning shows up - iframe src is unsecured and different domain that the containing page 如何获取iframe src包含不同域名的iframe的contentWindow.location? - How can I get the contentWindow.location of the iframe, where the iframe src contains different domain name? 无法从其他域中的页面访问iframe内容 - Iframe contents not accessible from a page in different domain iframe当前src,具有从其他域到同一域的重定向 - Iframe current src with a redirection from a different domain to same domain 具有不同域src的HTML iframe:如何禁用点击但保持悬停状态不变 - HTML iframe with different domain src: how to disable the click but keep the hover intact src是base64编码时,如何获取iframe内容高度? - How to get iframe contents height when src is base64 encoded? 是否可以将表单的目标设置为具有不同域Src的Iframe - Is it Possible to set the Target of a form to an Iframe which has Src of a different domain 如何使用jQuery操作iframe对象 - How to manipulate iframe objects using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM