How to create a new table row after every 4 cells within a forloop in JavaScript?
Not sure how to use the modulus operator to achieve this:
<table border="1">
<tr>
for (i = 0; i < someVar.length; i++) {
if (i % 4) document.write('</tr><tr>');
document开发者_JS百科.write('<td>Hi</td>'+i):
}
</tr>
</table>
<table border="1">
<tr>
for (i = 0; i < someVar.length; i++) {
document.write('<td>Hi</td>'+i);
if (i % 4 == 3) document.write('</tr><tr>');
}
</tr>
</table>
if(i%4==3) document.write('</tr><tr>');
and after the for loop, there should be something what adds empty cells to the last row, i.e.
while(i++%4!=0) document.write('<td> </td>');
<table id="tabid" border="1" >
<script language="javascript1.1" type="text/javascript" >
for (i = 0; i < 12; i++) {
if (i % 4==0) document.write('</tr><tr>');
document.write('<td>Hi'+i+'</td>');
}
</script>
</table>
Works fine for me
for (var i = 0; i < 10; i++) {
document.write("<td>Example</td>");
if (i % 4 == 3) {
document.write("</tr><tr>");
}
}
See example here: http://jsfiddle.net/UeThn/28/
精彩评论