我的 javascript 代码中有以下行:
if(document.defaultView && document.defaultView.getComputedStyle){
strValue = document.defaultView.getComputedStyle(oElm).getPropertyValue(strCssRule);
} // ...
检索我的节点的 CSS 值(例如:)strCssRule = 'color'
。它适用于火狐。它仅适用于 Chrome 上的静态页面上的简单示例。当试图将它集成到我的复杂应用程序中时,返回的每个字段CSSStyleDeclaration
都是空的。
oElem
使用 jQuery ( ) 检索节点oElem = $(my_selector).empty().get(0));
。工作示例和我的应用程序之间的区别(至少是我能想到的唯一区别)是我的节点在计算时尚未集成到 DOM 中(仍在生成完整的 HTML)。
即使在 HTML delectation 中明确设置 CSS(例如我的节点是<div style="width:100px;"></div>
)时,我也尝试过,CSSStyleDeclaration 对象仍然为空。
编辑:如 CBroe 所述,问题在于该元素尚未链接到 DOM。我想这取决于浏览器的实现如何处理这种情况。我不知道 Firefox 是如何使它工作的,但我会找到别的东西。