动态创建按钮

时间:2016-10-14 09:34:24

标签: javascript html element getelementbyid

我有一个函数(在最底部看到)创建一个HTML表,根据数组的内容,它将用X行填充它。每行有2个单元格,该位置的数组值和旁边的按钮。

我希望能够点击这些按钮并删除表格中的特定行。

但是,我无法在点击事件中使用标准:

function unMatchButtonClicked(){
  var button = document.getElementById('unmatch').onclick;

}

因为它会抛出id不存在的错误,并且因为我有可能的X行数,所以我需要某种for循环。

我的伪造尝试是:

for (var i=0; i < table.length; i++){
  var button = document.getElementById('unmatch')
  if (button.clicked){
  remove row}
}

尽管如此,我仍然无法做到这一点。

只有纯粹的JS解决方案,请没有Jquery。

编辑:

function makeHTMLMatchesTable(array){
  var table = document.createElement('table');
    for (var i = 0; i < array.length; i++) {
      var row = document.createElement('tr');
      var cell = document.createElement('td');
      cell.textContent = array[i];
      row.appendChild(cell);
    cell = document.createElement('td');
      var button = document.createElement('button');
      button.setAttribute("id", "unMatchButton" +i);
      cell.appendChild(button);
      row.appendChild(cell);
      table.appendChild(row);
    }
    return table;
}

3 个答案:

答案 0 :(得分:1)

使用addEventListener()创建元素时添加事件:

...
var button = document.createElement('button');
button.setAttribute("id", "unMatchButton" +i);

button.addEventListener("click", clickEventFunction, false);
...

希望这有帮助。

function makeHTMLMatchesTable(array) {
  var table = document.createElement('table');
  table.setAttribute("border", 1);

  for (var i = 0; i < array.length; i++) {
    var row = document.createElement('tr');
    var cell = document.createElement('td');
    cell.textContent = array[i];
    row.appendChild(cell);
    cell = document.createElement('td');
    
    var button = document.createElement('button');
    button.setAttribute("id", "unMatchButton" + i);
    button.textContent = "Delete";

    //click Event 
    button.addEventListener("click", delete_row, false);
    
    cell.appendChild(button);
    row.appendChild(cell);
    table.appendChild(row);
  }

  return table;
}

function delete_row() {
    this.parentNode.parentNode.remove();
}

document.body.appendChild(makeHTMLMatchesTable(['Cell 1','Cell 2','Cell 3','Cell 4']));

答案 1 :(得分:1)

click上添加<table>处理程序。然后,如果event.target触发了点击,您可以查看<button>。如果是,则向上移动DOM,直到您到达周围的<tr>元素并在其上调用.remove()

function makeHTMLMatchesTable(array) {
  var table = document.createElement('table');

  for (var i = 0; i < array.length; i++) {
    var row = document.createElement('tr');
    var cell = document.createElement('td');
    cell.textContent = array[i];
    row.appendChild(cell);
    cell = document.createElement('td');
    var button = document.createElement('button');
    button.setAttribute("id", "unMatchButton" + i);
    button.textContent = "Remove";
    cell.appendChild(button);
    row.appendChild(cell);
    table.appendChild(row);
  }

  table.addEventListener("click", removeRow, false);

  return table;
}

function removeRow(evt) {
  if (evt.target.nodeName.toLowerCase() === "button") {
    evt.target.parentNode.parentNode.remove();  // .parentNode.parentNode == <tr>
  }
}

document.body.appendChild(makeHTMLMatchesTable([1, 2, 3, 4]));

答案 2 :(得分:0)

详细信息在来源中进行了评论。还有一个PLUNKER

<!DOCTYPE html>
<html>

<head>
  <style>
    table,
    td {
      border: 1px solid red;
    }
    button {
      height: 24px;
      width: 24px;
    }
  </style>
</head>

<body>

  <script>
    var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];

    function makeHTMLMatchesTable(array) {
      var table = document.createElement('table');
      for (var i = 0; i < array.length; i++) {
        var row = document.createElement('tr');
        var cell = document.createElement('td');
        cell.textContent = array[i];
        row.appendChild(cell);
        cell = document.createElement('td');
        var button = document.createElement('button');
        button.setAttribute("id", "unMatchButton" + i);
        cell.appendChild(button);
        row.appendChild(cell);
        table.appendChild(row);
      }
      // This is added to comlete this function
      return document.body.appendChild(table);
    }

    makeHTMLMatchesTable(array1);

     // Reference table
    var table = document.querySelector('table');

    /*
    | - Add an eventListener for ckick events to the table
    | - if event.target (element clicked; i.e. button) 
    |   is NOT the event.currentTarget (element that
    |   is listening for the click; i.e. table)...
    | - ...then assign a variable to event.target's
    |   id (i.e. #unMatchButton+i)
    | - Next extract the last char from the id (i.e. from
    |   #unMatchButton+i, get the 'i')
    | - Then convert it into a real number.
    | - Determine the row to which the button (i.e. event
    |   .target) belongs to by using the old rows method.
    | - while row still has children elements...
    | - ...remove the first child. Repeat until there are
    |   no longer any children.
    | - if the parent of row exists (i.e. table which it 
    |   does of course)...
    | - ...then remove row from it's parents
    */
    table.addEventListener('click', function(event) {
      if (event.target !== event.currentTarget) {
        var clicked = event.target.id;
        var i = clicked.substr(-1);
        var idx = Number(i);
        var row = this.rows[idx];
        while (row.children > 0) {
          row.removeChild(row.firstChild);
        }
        if (row.parentNode) {
          row.parentNode.removeChild(row);
        }
        return false
      }
    }, false);
  </script>
</body>


</html>