TS2339: Property 'style' does not exist on type 'Element'

后端 未结 3 730
悲&欢浪女
悲&欢浪女 2020-12-19 15:38

Here\'s the code:

const test = Array.from(document.getElementsByClassName(\'mat-form-field-infix\'));
test.forEach((element) => {
    element.outerHTML =          


        
3条回答
  •  情深已故
    2020-12-19 16:02

    You need a typecast:

    Array.from(document.getElementsByClassName('mat-form-field-infix') as HTMLCollectionOf)
    

    That's because getElementsByClassName only returns HTMLCollection, and Element does not have a styleproperty. The HTMLElement however does implement it via it's ElementCSSInlineStyle extended interface.

    Note that this typecast is typesafe in the way that every Elementis either a HTMLElement or an SVGElement, and I hope that your SVG Elements don't have a class.

提交回复
热议问题