使用javascript在Chrome上获取当前样式时太慢

时间:2012-02-21 12:28:20

标签: performance google-chrome computed-style

我遇到了一个愚蠢的问题。下面是我获取计算dom风格的代码。它在FireFox甚至IE上运行良好(惊讶)!但在chrome v17上速度太慢(慢20倍)。如何避免这个问题?

getCurrentStyle = function(el)
{
    if (el.currentStyle)
        return el.currentStyle;
    else if (window.getComputedStyle)
        return document.defaultView.getComputedStyle(el,null);
    return null;
}

请帮忙!

1 个答案:

答案 0 :(得分:3)

这是一个Chrome错误。这样我们就可以避免使用这个函数了:

document.defaultView.getComputedStyle(el,null)