gpt4 book ai didi

javascript - 将按钮添加到动态生成的 HTML 表格中

转载 作者:行者123 更新时间:2023-12-03 02:52:08 24 4
gpt4 key购买 nike

我正在尝试使用 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]

只是想知道是否有办法将图像按钮添加到动态生成的表格中?选择按钮后,我想获取该行的值并将其作为参数传递给另一个函数。

最佳答案

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

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

关于javascript - 将按钮添加到动态生成的 HTML 表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47806846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com