简体   繁体   English

我该如何解决“属性'样式'不存在类型'元素'错误?

[英]How can i solve a "Property 'style' does not exist on type 'Element' error?

I really need help to solve this error.我真的需要帮助来解决这个错误。 I wrote the following JavaScript code below to enable my tab indicator to show on a clicked tab when clicked on but all I am getting is an error which says: "Property 'style' doesn't exist on type 'Element'".我在下面编写了以下 JavaScript 代码,以使我的选项卡指示器在单击时显示在单击的选项卡上,但我得到的只是一个错误,上面写着:“类型'元素'上不存在属性'样式'”。 The tabs work perfectly fine, I can switch from one tab to the other.标签工作得很好,我可以从一个标签切换到另一个。 I have seen similar questions but my issue wasn't resolved following their lead suggestions.我见过类似的问题,但我的问题没有按照他们的主要建议得到解决。 I am currently building an angular project and i wrote my JavaScript in the ngInit(){} function in my .ts file.我目前正在构建一个 Angular 项目,我在 .ts 文件的 ngInit(){} 函数中编写了我的 JavaScript。 The code is below:代码如下:

ngOnInit() {
    let tabPanes = this._class('tab-header')[0].getElementsByTagName('div');

    for (let i = 0; i < tabPanes.length; i++) {
      tabPanes[i].addEventListener('click', () => {
        this._class('tab-header')[0]
          .getElementsByClassName('active')[0]
          .classList.remove('active');
          tabPanes[i].classList.add("active");

          this._class('tab-indicator')[0].style.top = 'calc(80px + ${i*50}px)'; // This flags a "property 'style' doe not exist on type 'Element'" error.

          this._class('tab-content')[0]
          .getElementsByClassName('active')[0]
          .classList.remove('active');

          this._class('tab-content')[0]
          .getElementsByTagName('div')[i]
          .classList.add('active');
      });
    }
  }

_class(name: any) {
    return document.getElementsByClassName(name);
  }

getElementsByClassName<\/code> returns an HTMLCollectionOf<Element><\/code> . getElementsByClassName<\/code>返回一个HTMLCollectionOf<Element><\/code> 。 There is no style<\/code> property on Element type and hence the error. Element 类型没有style<\/code>属性,因此出现错误。

You can type cast it to collection of HTMLElement and that should resolve the issue:您可以将其类型转换为 HTMLElement 集合,这应该可以解决问题:

_class(name: any): HTMLCollectionOf<HTMLElement> {
    return document.getElementsByClassName(name) as HTMLCollectionOf<HTMLElement>;
}

Change _class method to :-将 _class 方法更改为:-

_class(name: any): NodeList<HTMLElement> {
    return document.getElementsByClassName(name) as NodeList<HTMLElement>;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决“'元素'类型上不存在属性'checkValidity'。” ts(2339) 错误 - How can i solve a "Property 'checkValidity' does not exist on type 'Element'." ts(2339) error 我该如何解决 - “订阅”类型上不存在“那么”属性 - How can I solve - Property 'then' does not exist on type 'Subscription' 类型“元素”上不存在属性“样式” - Property 'style' does not exist on type 'Element' 如何解决类型“从不”中不存在的属性 - How to solve Property does not exist in type 'never' 如何修复 Typescript 中的“元素”类型错误不存在属性? - How to fix property does not exist on type 'Element' error in Typescript? TS2339:类型“元素”上不存在属性“样式”? - TS2339: Property 'style' does not exist on type 'Element'? TS2339:“元素”类型上不存在属性“样式” - TS2339: Property 'style' does not exist on type 'Element' 如何解决“IntrinsicAttributes &amp; Function”类型上不存在的类型属性“”? - How to solve Type Property '' does not exist on type 'IntrinsicAttributes & Function'? 解决打字稿错误的最佳方法 - 类型上不存在属性 - Best way to solve typescript error - property does not exist on type 我该如何克服错误:“选择”类型上不存在“节点”属性 <string[]> “? - How can I overcome the error: Property 'nodes' does not exist on type 'Selection<string[]>'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM