簡體   English   中英

Typescript:屬性“數據”在HTMLElement類型上不存在

[英]Typescript : Property 'data' dose not exist on type HTMLElement

我嘗試獲取所有在打字稿中都有類名稱的要素,並需要檢查每個元素的data屬性,這是我到目前為止的結果:

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();
     }
   }
 }

但是在我的if條件行中,我在data Property 'data' dose not exist on type HTMLElement上遇到此錯誤, Property 'data' dose not exist on type HTMLElement

我做錯了嗎?

因為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)設置值。

您可以在MDN頁面上查看有關如何正確使用數據屬性的教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM