简体   繁体   English

打字稿错误“ HTMLElement”类型不存在属性。 任何

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

I am trying to create a table using javascript inside typescript but I am getting error: 我试图在打字稿中使用javascript创建表,但出现错误:

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

My code which is generating a table is like this: 我的生成表的代码是这样的:

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

I am getting errors for two properties createTHead() and insertRow() I followed few answers in stackoverflow I used this: 我在两个属性createTHead()和insertRow()上遇到错误,在stackoverflow中遵循以下几个答案:

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

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

I tried these following things I don't know why I am getting this error: 我尝试了以下这些操作,但我不知道为什么会收到此错误:

Answer to my solution was very simple I need to assign what type of element my table is. 解决方案的答案非常简单,我需要分配表的元素类型。 So answer to my problem is: 所以我的问题的答案是:

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

So I added HTMLTableElement and this was the solution to my problem. 因此,我添加了HTMLTableElement,这是解决我的问题的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM