簡體   English   中英

document.html.className在angular2中不起作用

[英]document.html.className not working in angular2

當我從body標簽中刪除一個類時,以下代碼可以正常工作。

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沒有這個html屬性。
這不是打字稿問題,而是JavaScript,請嘗試在控制台中運行此腳本:

console.log(document.html);

並且您將得到undefined

要獲得對DOM的html部分的引用,您需要使用document.documentElement屬性( 類型定義 MDN ):

console.log(document.documentElement);

暫無
暫無

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

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