簡體   English   中英

打字稿錯誤“ HTMLElement”類型不存在屬性。 任何

[英]Typescript error Property does not exist on type 'HTMLElement'. any

我試圖在打字稿中使用javascript創建表,但出現錯誤:

[ts] Property 'insertRow' does not exist on type 'HTMLInputElement'.
any

我的生成表的代碼是這樣的:

  createTable(chart){
  var table = document.createElement("TABLE");    
  var row,header,cell1, cell2;
  var data = chart.options.data;

  table.style.border = "1px solid #000"; 
  header = table.createTHead();
  row = header.insertRow(0);    
  cell1 = row.insertCell(0);
  cell2 = row.insertCell(1);
  cell1.style.border = "1px solid #000"; 
  cell2.style.border = "1px solid #000"; 
  cell1.innerHTML = "<b>X-Value</b>"; 
  cell2.innerHTML = "<b>Y-Value</b>"; 

  for(var i = 0; i < data.length; i++){
    for(var j = 0; j< data[i].dataPoints.length; j++){
      row = table.insertRow(1);

      cell1 = row.insertCell(0);
      cell2 = row.insertCell(1);
      cell1.style.border = "1px solid #000"; 
      cell2.style.border = "1px solid #000"; 

      cell1.innerHTML = data[i].dataPoints[j].x;
      cell2.innerHTML = data[i].dataPoints[j].y; 
    }
  }    
  document.getElementById("chartContainer").appendChild(table);
}

我在兩個屬性createTHead()和insertRow()上遇到錯誤,在stackoverflow中遵循以下幾個答案:

(document.createElement("TABLE")) as HTMLInputElement;

(table.createTHead()) as HTMLBodyElement;
(table.createTHead()) as HTMLInputElement;

我嘗試了以下這些操作,但我不知道為什么會收到此錯誤:

解決方案的答案非常簡單,我需要分配表的元素類型。 所以我的問題的答案是:

  createTable(chart){
  var table = document.createElement("TABLE")  as HTMLTableElement;    
  var row,header,cell1, cell2;
  var data = chart.options.data;
  table.style.border = "1px solid #000"; 
  header = table.createTHead();
  row = header.insertRow(0);    
  cell1 = row.insertCell(0);
  cell2 = row.insertCell(1);
  cell1.style.border = "1px solid #000"; 
  cell2.style.border = "1px solid #000"; 
  cell1.innerHTML = "<b>X-Value</b>"; 
  cell2.innerHTML = "<b>Y-Value</b>"; 

  for(var i = 0; i < data.length; i++){
    for(var j = 0; j< data[i].dataPoints.length; j++){
      row = table.insertRow(1);
      cell1 = row.insertCell(0);
      cell2 = row.insertCell(1);
      cell1.style.border = "1px solid #000"; 
      cell2.style.border = "1px solid #000"; 

      cell1.innerHTML = data[i].dataPoints[j].x;
      cell2.innerHTML = data[i].dataPoints[j].y; 
    }
  }    
  document.getElementById("chartContainer").appendChild(table);
 }

因此,我添加了HTMLTableElement,這是解決我的問題的方法。

暫無
暫無

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

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