首页 >web前端 >css教程 >如何从跨域 iframe 中检索计算的样式值?

如何从跨域 iframe 中检索计算的样式值?

Linda Hamilton
Linda Hamilton原创
2024-11-30 11:56:11345浏览

How to Retrieve Computed Style Values from Cross-Domain Iframes?

检索跨域元素的计算样式值

使用来自不同域的 iframe 时,访问计算样式信息可能具有挑战性。要获取计算样式,请按照下面提供的综合解决方案进行操作。

WebKit 浏览器

对于基于 WebKit 的浏览器,请使用以下代码片段:

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyValue("height")

或者,您可以以 CSS 形式访问计算值text:

window.getComputedStyle(document.getElementById("frameId"), null).getPropertyCSSValue("height").cssText

Internet Explorer

对于 Internet Explorer,使用 'currentStyle' 属性:

document.getElementById("frameId").contentDocument.body.currentStyle.height

Iframe 内容

但是,要获取计算出的样式iframe 的内容,您需要使用以下步骤更深入地导航到 DOM:

  1. 获取对 iframe 文档的引用:

    var iframeDoc = document.getElementById("frameId").contentDocument;
  2. 根据所述的浏览器类型执行适当的代码

示例

一旦有了计算样式,您就可以按如下方式访问它:

iframeDoc.getElementById("brshtml").style.height

请注意,如果目标文档中不存在“brshtml”id,则可能需要调整。

以上是如何从跨域 iframe 中检索计算的样式值?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn