文本对齐“中心”不适用于表标题

时间:2019-03-27 12:40:37

标签: javascript arrays for-loop text-align

我已经创建了一个表,并且正在尝试使用“ align ='center'”将表头的文本对齐到中心,但是即使它适用于表数据也无法正常工作。

另一个问题是,在使用嵌套的for循环向表单元格添加边框时,我在通过chrome进行调试时遇到了此错误:

"Cannot read property 'style' of undefined"

同一代码可以在没有for循环的情况下编写10次代码。

这是完整的功能:

function makeBookingDetailsTable(rs) 

{  
        var arry = new Array();
        arry = rs.split(", ");
        var table = "<table id='myTable'>";
        var content="";

//ALIGN DOESN'T WORK FOR 'th'
         content += "<tr><th align='center'>" + "Booking Id" + "</th>";
         content += "<th align='center'>" + "CCA Sent" + "</th>";
         content += "<th align='center'>" + "CCA Acknowledged" + "</th>";
         content += "<th align='center'>" + "Folio Received" + "</th></tr>";



 //ALIGN WORKS FOR 'td'        
         for (var i = 0; i < arry.length; i++)
             {

   content += "<tr><td align='center'>" + arry[i] + "</td>";
content += "<td align='center'><select><option value='Yes'>Yes</option><option value='No'>No</option></select></td>";
content +="<td align='center'><select><option value='Yes'>Yes</option><option value='No'>No</option></select></td>"; 
content +="<td align='center'><select><option value='Yes'>Yes</option><option value='No'>No</option></select></td>";    
             }         
         content += "</table>";
         table += content;

        $(document.body).append(table);

        document.getElementById('myTable').style.width="80%";

        document.getElementById('myTable').style.border="2px solid black";

for ( var i=0; i<=arry.length; i++)
{
    for  (var j=0; j<5; j++)
    {
    document.getElementById("myTable").rows[i].cells[j].style.border = "2px solid black" ;

//OUTER LOOP RUNS ONLY ONCE. IF I USE 'rows[0]', 'rows[1]' ETC. IN PLACE OF 'rows[i]' IN ABOVE LINE, THINGS WORK FINE.

     }
}    
}

请帮助我!

0 个答案:

没有答案