Javascript Typescript:Property';数据';类型HtmleElement上不存在

Javascript Typescript:Property';数据';类型HtmleElement上不存在,javascript,angular,typescript,Javascript,Angular,Typescript,我尝试获取所有在typescript中具有类名的元素,并且需要检查每个元素的数据属性,这就是我到目前为止所做的: let getTopElements :NodeListOf<HTMLElement> = document.querySelectorAll('.timeline-row'); var newArr: HTMLElement[] = Array.prototype.slice.call(getTopElements); if (getTopElements){

我尝试获取所有在typescript中具有类名的元素,并且需要检查每个元素的数据属性,这就是我到目前为止所做的:

let getTopElements :NodeListOf<HTMLElement>  = document.querySelectorAll('.timeline-row');
var newArr: HTMLElement[] = Array.prototype.slice.call(getTopElements);

if (getTopElements){
  for (let element of newArr){
    if (element.data('options') && element.data('options').type === 'TOP')   {
       this.player.currentTime(element.data('options').end).pause();
     }
   }
 }
let getTopElements:NodeListOf=document.querySelectorAll('.timeline行');
var newArr:HTMLElement[]=Array.prototype.slice.call(getTopElements);
if(getOpelements){
for(newArr的let元素){
if(element.data('options')&&element.data('options')。type=='TOP'){
this.player.currentTime(element.data('options').end).pause();
}
}
}
但是在我的if条件行中,我在data
上得到了这个错误,而在类型HTMLElement上不存在属性'data'

我做错了吗?

因为
data()
是jQuery中获取数据属性的方法。您应该使用
dataset
属性来修改和读取数据属性。还要记住,只能在数据属性中存储字符串值:

 const data = element.dataset;
 data.optionsType = 'TOP';
 if (data.optionsType === 'TOP')   {
   this.player.currentTime(data.optionsEnd).pause();
 }
另一个选项是使用
getAttribute('data-*')
获取值,或使用
setAttribute('data-*',value)
设置值

您可以在中查看如何正确使用数据属性的教程。

因为
data()
是jQuery中获取数据属性的方法。您应该使用
dataset
属性来修改和读取数据属性。还要记住,只能在数据属性中存储字符串值:

 const data = element.dataset;
 data.optionsType = 'TOP';
 if (data.optionsType === 'TOP')   {
   this.player.currentTime(data.optionsEnd).pause();
 }
另一个选项是使用
getAttribute('data-*')
获取值,或使用
setAttribute('data-*',value)
设置值

您可以查看以获取如何正确使用数据属性的教程