简体   繁体   中英

document.html.className not working in angular2

Following code works fine when I'm removing a class from body tag.

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

But I can't remove a class from HTML tag as below.

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

It gives following error in the first line of the function.

Property 'html' does not exist on type 'Document'.

Any help?

That's because document does not have this html property.
That's not a typescript issue, it's javascript, try to run this in your console:

console.log(document.html);

And you'll get undefined .

To get a reference to the html part of the DOM you need to use the document.documentElement property ( the type definition , MDN ):

console.log(document.documentElement);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM