Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向动态生成的HTML表添加按钮_Javascript_Html_Html Table - Fatal编程技术网

Javascript 向动态生成的HTML表添加按钮

Javascript 向动态生成的HTML表添加按钮,javascript,html,html-table,Javascript,Html,Html Table,我试图使用JavaScript为动态生成的HTML表的每一行添加一个按钮。这是我的密码: // helper function function addCell(tr, text) { var td = tr.insertCell(); td.innerHTML = text; return td; } // insert data list.forEach(function (item) { var row = table.insertRow(

我试图使用JavaScript为动态生成的HTML表的每一行添加一个按钮。这是我的密码:

// helper function        
function addCell(tr, text) {
    var td = tr.insertCell();
    td.innerHTML = text;
    return td;
}

// insert data
list.forEach(function (item) {
    var row = table.insertRow();
    var button = document.createElement('button'); 
    var bText = document.createTextNode('View'); 
    button.appendChild(bText); 
    addCell(row, item.itemName);
    addCell(row, '$ ' + item.total.toFixed(2));
    addCell(row, button);
});
我设法打印出除按钮外的所有其他字段。按钮栏只是简单地打印出以下内容:

[object HTMLButtonElement]
只是想知道是否有必要向动态生成的表中添加图像按钮?选择按钮后,我想获取行的值,并将其作为参数传递给另一个函数。

。我不知道你想要什么。这样你就可以到达终点

var previousitem=null;
function removeRowbyItem(item) {
      item.parentElement.parentElement.style.backgroundColor="red";
      if(previousitem!=null){
         previousitem.style.backgroundColor="";
         console.log(previousitem);
      }
    previousitem=item.parentElement.parentElement;
}
removeRow.innerHTML = "click me";
removeRow.onclick = function() {
  removeRowbyItem(this);
};

您可以使用
td.appendChild(按钮)


你能分享提琴或片段吗?有没有办法设置所选行的背景色?更新我的答案@hyperfkcbSorry,但有一个小问题,因为假设该行仅限于单选。在突出显示新行之前,是否仍要清除上一行?
function addCell(tr, text) {
    var td = tr.insertCell();
    td.innerHTML = text;
    return td;
}
//for append button
function addButtonToCell(tr, button) {
    var td = tr.insertCell();
    td.appendChild(button);
    return td;
}
// insert data
list.forEach(function (item) {
    var row = table.insertRow();
    var button = document.createElement('button'); 
    var bText = document.createTextNode('View'); 
    button.appendChild(bText); 
    addCell(row, item.itemName);
    addCell(row, '$ ' + item.total.toFixed(2));
    addButtonToCell(row, button);
});