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

EDIT : ボタン押下ハンドラ関数のサンプル

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 に答える