在按鈕點擊事件中傳入 this,可先取得按鈕節點,再用 parentNode 往上找到 tr,接著透過 cells 或 getElementsByTagName 取得 td 內容。
function getTableContent(node) {
var tr = node.parentNode.parentNode;
alert(tr.rowIndex);
alert(tr.cells[0].innerText);
}
也可以先用 document.getElementById 取得 table,再用 getElementsByTagName('tr') 取得指定列,最後讀取 firstChild.innerHTML 或 cell 的 innerText。