跳到表格中的下一行

时间:2012-02-28 05:19:22

标签: javascript html-table

这是我到目前为止所拥有的。它将一行中的所有数字和每个数字都放在一个单独的单元格中。我希望它在每行中发布6个数字,并在每个单元中发布每个数字。我该怎么做?

function primeNumbers()
{
document.write("<table cols='10' align='center' border='1'>");
document.write("<tr>");
document.write("<th  colspan='10' align='center'>   Prime Numbers   </th>");
document.write("</tr>");
var numbersSize = parseInt(prompt("Please enter the last number for the Sieve of Eratosthenes "));
for(i = 0 ; i <= numbersSize; i++)
{

        for( c = 2; c <= i - 1 || i == 2; c++ )
        {

            if(i % c != 0 && c == i - 1)
            {   
                document.write("<td>" +i+ "</td>");
            }
            if(i == 2)
            {
                document.write("<td>" + i + "</td>");
            }
            if(i % c == 0)
            {
                break;
            }

        }
}

document.write("</table>");
}

1 个答案:

答案 0 :(得分:0)

试试这个:

function primeNumbers() {
           document.write("<table cols='10' align='center' border='1'>");
           document.write("<tr>");
           document.write("<th  colspan='10' align='center'>   Prime Numbers   </th>");
           document.write("</tr>");
           var numbersSize = parseInt(prompt("Please enter the last number for the Sieve of Eratosthenes "));
           var cellcount = 0; 
           document.write("<tr>");
           for(i = 0 ; i <= numbersSize; i++) {

               for (c = 2; c <= i - 1 || i == 2; c++) {
                   document.write("<tc>");
                   if (i % c != 0 && c == i - 1) {
                       document.write("<td>" + i + "</td>");
                       cellcount++;
                   }
                   if (i == 2) {
                       document.write("<td>" + i + "</td>");
                       cellcount++;
                   }
                   if (i % c == 0) {
                       break;
                   }
                   document.write("</tc>");
                   if (cellcount == 6) {
                       document.write("</tr><tr>");
                       cellcount=0;
                   }
               }
           }
           document.write("</table>");
       }