2017-07-24 43 views
0

當我從身體標記中移除一個類時,以下代碼正常工作。document.html.className不能在angular2中工作

switchTheme(themeCode: string) { 
    document.body.className = ''; 
    document.querySelector('body').classList.add(themeCode); 
    } 

但我不能刪除HTML標籤類,如下。

switchTheme(themeCode: string) { 
    document.html.className = ''; 
    document.querySelector('html').classList.add(themeCode); 
} 

它在函數的第一行給出以下錯誤。

屬性'html'在類型'Document'上不存在。

任何幫助?

回答

2

那是因爲document沒有這個html屬性。
這不是一個打字稿的問題,它的JavaScript,嘗試在控制檯中運行以下命令:

console.log(document.html); 

,你會得到undefined

要到DOM的html部分的引用您需要使用document.documentElement屬性(the type definitionMDN):

console.log(document.documentElement); 
+0

所以,document.documentElement.className = '' 是解決方案。謝謝 –