2017-03-31 50 views
0

我一直在想轉換一個JavaScript的Web表格打字稿,並一直無法工作,如何涉及的錶行,語句轉換如以下幾點:在打字稿處理表中的行

let body = document.getElementById("selectStackBody"); 

let rowCount = body.rows.length; 

let row = body.insertRow(rowCount); 

body.deleteRow(i); 

我可以請參閱如何通過執行以下操作獲取主體:

let body = <HTMLBodyElement>document.getElementById("selectStackBody"); 

但我看不到如何獲取行的正確語法。

+0

有什麼問題嗎?什麼錯誤被拋出?你有什麼嘗試? – Pytth

+0

HTMLBodyElement接口沒有'rows'屬性。然而,'HTMLTableSectionElement'接口('tbody'元素使用的接口)的確如此。如果您顯示您試圖運行此代碼的HTML,那麼幫助您會更容易。 –

回答

1

你不一定要在這裏做任何特定的TypeScript。基本的DOM語法可以爲你做到這一點。

如果document.getElementById("selectStackBody")給出表體節點,那麼只需執行body.childNodes.length即可獲取行數。

同樣,要追加一行,使用DOM語法來添加一個子節點。

這裏有一個例子:

const body = document.getElementById('selectStackBody') 

// Total number of rows 
let numberOfRows = body.childNodes.length 

// Add a new row 
let newRow = document.createElement('tr') 
body.appendChild(newRow) 

// Remove the first row 
body.removeChild(body.childNodes[0]) 
+0

感謝您爲規避問題提供幫助。我放心了! –