【问题标题】:How to get the attribute of outer tag property from iframe [duplicate]如何从iframe中获取外部标签属性的属性[重复]
【发布时间】:2016-05-24 16:25:23
【问题描述】:
    <html dir="LTR">
    <head></head>
    <body> 
      <iframe id="id_description_iframe" class="rte-zone" height="200" frameborder="0" title="description">
        <html>
          <head>
            <script>
              alert('document.querySelector("html").dir');
            </script>
          </head>
          <body class="frameBody">
            test<br/>
          </body>
        </html>
      </iframe>
    </body>
  </html>

我需要的是 html 中 dir 的值,即“LTR”。

我在 iframe 的代码中尝试了 "document.querySelector("html").dir",它在查找第二个 html 标记时返回 ""。

【问题讨论】:

    标签: javascript html iframe directory


    【解决方案1】:

    由于它在不同的文档中,您无法找到以document 开头的它。

    但假设你的 iframe 和它的父级在同一个来源,你可以通过parent.document.querySelector('html').dir 或更直接地通过parent.document.documentElement.dir 访问它(因为documentElement 在HTML 文档中是html 元素):

    alert(parent.document.documentElement.dir);
    

    如果 iframe 与其父级不在同一来源,您将无法访问它。

    【讨论】:

    • @Gothdo:好骗;完毕。上面还有一些有用的附加信息,所以我会留下它。
    猜你喜欢
    • 2017-02-27
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2020-09-23
    • 2023-03-30
    • 2015-09-16
    相关资源
    最近更新 更多