[英]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.