我的代码是:
<!DOCTYPE html>
<html>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<iframe src="http://www.amway.in/Shopping/Products.aspx?pid=101154IDK&ctg=Nutrilite">
<p>Your browser does not support iframes.</p>
</iframe>
</body>
<style>
iframe::-webkit-scrollbar {
display: none;
}
iframe {
width: 600px;
height: 800px;
}
</style>
<script>
$(window).bind("load", function() {
$("iframe").contents().find(".left-nav").css("display","none");
});
</script>
</html>
我想将 css 属性“display”更改为“none”,但它在页面加载后显示错误。
- 未捕获的安全错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止了来源为“null”的框架访问来源为“http://www.amway.in”的框架。请求访问的帧协议(protocol)为“file”,被访问的帧协议(protocol)为“http”。协议(protocol)必须匹配。
最佳答案
那是因为同源政策。
The same-origin policy restricts how a document or script loaded from one origin can interact with a resource from another origin. [...]
Cross-origin script API access
JavaScript APIs such as
iframe.contentWindow
,window.parent
,window.open
andwindow.opener
allow documents to directly reference each other. When the two documents do not have the same origin, these references provide limited access to the Window and Location objects. Some browsers allow access to more properties than the spec allows. You can usewindow.postMessage
instead to communicate between documents.
您可以阅读有关此主题的更多信息 here .
关于jquery - iframe 显示 :none not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590492/