Assigned expression type string is not assignable to type CSSStyleDeclaration

From:https://stackoverflow.com/questions/51030396/cost-benefits-of-using-different-ways-to-style-html-elements-with-javascript

Assigned expression type string is not assignable to type CSSStyleDeclaration

According to mdn, HTMLElement.style should be treated as read-only and not used to set a style attribute string; instead it is a CSSStyleDeclaration, as IntelliJ has informed you.

If you're dead set on specifying style as a string, you can do so using, e.g., element.style.cssText = 'border: 2px solid red'.

EDIT: to further narrow the answer to your question, the consequence is that you no longer have access to the CSSStyleDeclaration object and as it is an unsupported means of setting style, you risk inconsistent results or broken code.

使用element.style.cssText代替element.style,element.style应该作为只读

原文地址:https://www.cnblogs.com/arrayblog/p/13895664.html