jquery - iframe 显示 :none not working

标签 jquery html css

我的代码是:

<!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 and window.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 use window.postMessage instead to communicate between documents.

您可以阅读有关此主题的更多信息 here .

关于jquery - iframe 显示 :none not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22590492/

相关文章:

javascript - 如何使 html 页面的一个区域重定向到另一个 url 而页面的其余部分保持不变?

javascript - 如何在几个 div 中迭代切换元素

html - 在编写文本的 Css 属性时未获得预期的输出

javascript - 我无法让我的 html 编码幻灯片正常工作

javascript - 如何在 jQuery 中添加 if 语句来检查属性是否存在于 JSON 文件中?

jquery - 如何将具有相似属性的子级附加到父级?

jquery:如何根据文本输入隐藏表格

html - 如何将所有类 ="..."属性扩展为样式 ="..."?

javascript - 如何在 SVG 路径上显示文本

css - 如何以慢速流畅地制作CSS3动画?