【问题标题】:Traverse from element within iframe to parent iframe TAG从 iframe 内的元素遍历到父 iframe TAG
【发布时间】:2011-12-07 04:02:04
【问题描述】:

我在页面上有多个 iframe,需要从 iframe 的 html 元素访问父 iframe 元素,以获取 iframe 的高度。

所以从这里开始:

var htmlElement = $('iframe').contents().find('html');

以下内容不起作用:

var iframeHeight = htmlElement.parents('iframe').attr('height');

有人可以帮忙吗?

【问题讨论】:

  • 试试 htmlElement.parent().height()
  • "需要从 iframe 的 html 元素中访问父 iframe 元素,以获取 iframe 的高度。"这就像在说“我需要向前并向后退以保持在同一个位置”,为什么不通过 iframe 执行 $('iframe').attr('height');each() 并抓住它们的高度?

标签: jquery iframe dom-traversal


【解决方案1】:

请原谅,编辑(基于How to access parent Iframe from javascript

var iframe = null;
parent.$("iframe").each(function(iel, el) {
  if(el.contentWindow === window)
      iframe = el;
});
if (iframe)
    iframe.height()

这需要父窗口(有 iframe 的那个)有 jQuery

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多