1

当我单击插入行按钮时,我希望删除按钮也与行一起默认。例如

function addRow(demo)
  {
    var x=document.getElementById(myTable)'
    var row=x.insertRow(0);
    var cell1=row.insertCell(0);
    var cell2=row.insertCell(1);
    cell1.innerHTML=demo;
    cell2.innerHTML=(Here I want the delete button to appear);        
  }

当按下删除按钮(已经在每一行中)时,我应该编写什么函数来删除该行?

4

1 回答 1

0
    cell1.innerHTML=demo;
    cell2.innerHTML=(Here I want the delete button to appear);        
}

可以改为:

    cell1.innerHTML=demo;
    var delBtn = document.createElement('input');
    delBtn.setAttribute('type', 'button');
    delBtn.setAttribute('value', 'Delete');
    delBtn.addEventListener('click', nameOfYourDelBtnHandlerFunction, false);
    cell2.appendChild(delBtn);
}

编辑:示例按钮按下处理程序函数

function onRowDeleteBtn()
{
    var pressedButton = this;                   // must attach the handler with addEventListener, otherwise 'this' will not hold the required value
    var parentCell = pressedButton.parentNode;  // get reference to the cell that contains the button
    var parentRow = parentCell.parentNode;      // get ref to the row that holds the cell, that holds the button
    var parentTbody = parentRow.parentNode;     // get ref to the tbody that holds the row, that holds the cell, that holds the button
    parentTbody.removeChild(parentRow);         // nuke the row element
}
于 2013-08-02T06:26:48.750 回答