簡體   English   中英

document.defaultView.getComputedStyle 和 window.getComputedStyle 有什么區別

[英]what's the difference bewteen document.defaultView.getComputedStyle and window.getComputedStyle

當獲取元素的樣式時,我們總是使用

if(document.defaultView && document.defaultView.getComputedStyle)檢查瀏覽器是否支持該方法。

為什么不使用if(window.getComputedStyle)

所以簡而言之,我們使用document.defaultView && document.defaultView.getComputedStyle的原因是我們想要一個跨瀏覽器的工作在每個元素上的方法來檢查它是否支持獲取計算樣式。

對於 Firefox 3.6 中的 iframe,簡單的if(window.getComputedStyle)將失敗(根據 Alex K. 評論中鏈接的文章)。

根據MDN defaultView不再需要

在許多代碼示例中,從 document.defaultView 對象中使用 getComputedStyle。 在幾乎所有情況下,這是不必要的,因為 getComputedStyle 也存在於 window 對象上。 很可能 defaultView 模式是人們不想為窗口編寫測試規范和制作在 Java 中也可用的 API 的組合。

Firefox 3.6 (2010/2011) 中存在一個需要defaultView修復的 錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM