【发布时间】:2012-03-14 17:49:00
【问题描述】:
我有一个表格,可以在新用户活跃时动态添加行。该函数将添加一个显示用户名的新行和一个用于激活灯箱以显示更多信息的按钮。名称和按钮放在一个单元格中,但我想右对齐按钮并左对齐单元格内的名称。我发现这篇文章Right align and left align text in same HTML table cell 展示了如何使用 HTML 中的 div 来完成此操作,但我需要动态执行此操作。有人猜吗?我尝试了以下代码,它可以工作,但不能证明我想要的名称和按钮。它只是将它们集中在单元格内。我会很感激任何帮助。
function addRow(tableID, user, phone, age, city, zipCode) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
tabbody=document.getElementsByTagName("tbody").item(2);
cell1 = document.createElement("TD"); //Create New Row
leftDiv = document.createElement("div"); //Create left div
leftDiv.id = "left"; //Assign div id
leftDiv.setAttribute("style", "float:left;width:50%;"); //Set div attributes
rightDiv = document.createElement("div"); //Create right div
rightDiv.id = "right"; //Assign div id
rightDiv.setAttribute("style", "float:right;width:50%;"); //Set div attributes
user_name = document.createTextNode(user + ' '); //Set user name
details_btn = document.createElement("button"); //Create details button
btn_txt = document.createTextNode("Details"); //Create button text
details_btn.appendChild(btn_txt); //Add "Details" to button text
details_btn.onclick = function(){moreInfo(user, phone, age, city, zipCode);}; //Assign button function
cell1.appendChild(leftDiv);
cell1.appendChild(user_name); //Add name to row
cell1.appendChild(rightDiv);
cell1.appendChild(details_btn); //Add button to row
row.appendChild(cell1); //Add row to table
tabbody.appendChild(row);
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}
【问题讨论】:
标签: javascript html html-table alignment