getComputedStyle替换:currentStyle(IE8/7)不起作用

getComputedStyle substitue: currentStyle (IE8/7) not working

本文关键字:不起作用 IE8 替换 currentStyle getComputedStyle      更新时间:2023-09-26

我的currentStyle有一个问题,我已经读到它是IE9之前针对getComputedStyle支持的修复程序。

我最近做了另一个关于引用多级导航菜单的顶级LI列表的帖子:

选择<李>带有香草JavaScript 的子节点,但不是子节点

现在,我需要能够测量李的宽度或高度,而在我得到的帮助下,我无法参考。它有效,但不能低于IE9。

以下是我尝试获得宽度的方法:

this.w=函数(elm){

var s = (window.getComputedStyle) ? window.getComputedStyle(elm, "") : elm.currentStyle;
return parseInt(s.width);

}

宽度返回为NaN

SCRIPT5007:无法获取未定义或空引用的属性"innerHTML"

我真的很感谢大家的帮助

elm.currentStyle.width在未指定宽度时返回"auto"

这正确地反映了当前的样式设置,但没有为您提供所需的值(正如您可能期望的getComputedStyle所做的那样)。

相反,使用elm.clientWidth

因此,在您的示例中,您可能会使用。。。

this.w =function( elm ){
    var s = (window.getComputedStyle) ? window.getComputedStyle(elm, "") : elm.clientWidth;
    return parseInt(s.width);
}

(在IE8中测试,但在IE7中未测试)