代码之家  ›  专栏  ›  技术社区  ›  ONE_FE

document.html。类名不在angular2中工作

  •  0
  • ONE_FE  · 技术社区  · 8 年前

    当我从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);
    }
    

    它在函数的第一行中给出了以下错误。

    类型“Document”上不存在属性“html”。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Nitzan Tomer    8 年前

    document html 所有物
    这不是typescript问题,这是javascript,请尝试在控制台中运行它:

    console.log(document.html);
    

    你会得到 undefined

    获取对 html DOM的一部分,您需要使用 document.documentElement the type definition , MDN

    console.log(document.documentElement);