2010-11-10 27 views
3

我想caculate元素的當前樣式:currentStyle在IE爲空

function cssIsLoaded(c) { 
    if (window.getComputedStyle) { 
     return window.getComputedStyle(c, null).display === "none"; 
    } 
    else if (c.currentStyle) { 

    } 

    return true; 
} 

(function() { 
    var cssload = document.createElement("div"); 
    cssload.className = "_css_loaded"; 
    checkLoaded(); 

    function checkLoaded() { 
     if (!cssIsLoaded(cssload)) setTimeout(function() { 
      checkLoaded(); 
     }, 20); 
     else blalbalblbalbalablbal(); 
    } 
})(); 

IE犯規進入第二個條件,c.currentStyle爲空......這是爲什麼?

+1

你在哪裏調用'cssIsLoaded()'?你傳遞了什麼元素?你可以在http://jsfiddle.net上創建一個例子嗎? – 2010-11-10 10:17:12

+0

我傳遞了一個我剛創建的DIV。一旦它的JS加載到網站的中,我就會調用該函數。 (在我創建DIV – Himberjack 2010-11-10 10:30:11

+0

之後,請正確顯示你是如何傳遞它的,這很可能是錯誤發生的地方。 – 2010-11-10 10:31:35

回答

7

在將元素添加到文檔之前,某個元素不會獲得其填充的currentStyle屬性,這有一定意義:直到將元素添加到文檔中,瀏覽器無法知道哪些現有樣式規則將應用於該文檔。

相關問題