简体   繁体   中英

apply inline css to <html> tag in typescript on click

I am new to typescript and just got confused that how to apply inline CSS to tag on click in typescript. I thought it was very simple but in typescript, things work differently as I tried with document.querySelector("html").style.background = "blue" but getting error like

 Object is possibly 'null'.  TS2531

       jumpToRelativePosition(anchors['chapter-main'], anchors['chapter-next']);
      document.querySelector("html").style.background = "blue

"
        ^

Please help me

Thanks in advance

when you need to have access to DOM Elements, you must always decalre them what element are they, HTMlElement is every type of element in HTML

const x = document.querySelector("html") as HTMLElement

if you will decalre with current elemt, for example you have input element mark as in ts HTMLInputElement

EDIT: Do Not Forget to include "DOM" in you lib options in your tsconfig.json

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