我有一个动态生成的表,因为它正在生成它的 TD,我想将第一个 TD 设置为一个按钮。下面是我的代码,显然不起作用。我记得我遇到的另一个问题是,我们可以使用 .html() 更改 div 的内容,但这在这里也不起作用。
代码:
var table = document.getElementById("userinfo");
var thead, tr, td;
table.appendChild(thead = document.createElement("thead"));
thead.appendChild(tr = document.createElement("tr"));
tr.appendChild(td = document.createElement("td"));
td.innerHTML = "Email";
tr.appendChild(td = document.createElement("td"));
td.innerHTML = "First Name";
tr.appendChild(td = document.createElement("td"));
td.innerHTML = "Last Name";
tr.appendChild(td = document.createElement("td"));
td.innerHTML = "Is Active?";
for (var i in data)
{
tr = document.createElement("tr");
tr.setAttribute("id", "row" + i);
if (i%2 == 0)
{
tr.setAttribute("style", "background:white");
}
var entry = data[i];
console.log(entry);
table.appendChild(tr);
tr.appendChild(td = document.createElement("td"));
td.setAttribute("html", "<input type=\"button\" class=\"btn\" value=\'" + entry.email + "\" onclick=\"" + chooseUser(entry) + "\"/>");
tr.appendChild(td = document.createElement("td"));
td.innerHTML = entry.first_name;
tr.appendChild(td = document.createElement("td"));
td.innerHTML = entry.last_name;
tr.appendChild(td = document.createElement("td"));
td.innerHTML = entry.isActive;
}