for循环限制列数而不是行数

时间:2013-11-18 20:49:03

标签: javascript loops for-loop document.write

我有代码循环并通过document.write显示。我想将显示限制为3列,但行数不受限制。我将通过下面的代码,但基本上,我必须使用的空间是3列的宽度,但我有超过3列的数据。所以我希望循环停在3并继续下一行,但是行数不受限制。代码贴在这里:

enter code here

<script type="text/javascript">
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/_SHARED/ApplicationData/Public/FlashWriter.aspx?RotatorGroupID=5357", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var x = xmlDoc.getElementsByTagName("jpg");


document.write("<table border='0' width='675' cellspacing='0' cellpadding='0'>");

document.write("<tr>");

for (i = 0; i < x.length; i++) {

    document.write("<td><table border='0' cellspacing='0' cellpadding='0' id='static' ><tr><td class='HOT969' colspan='3'><a href='");
    document.write(x[i].attributes.getNamedItem("link").value);
    document.write("'><img width='190' height='125' class='image' src='");
    document.write(x[i].attributes.getNamedItem("image").value);
    document.write("'><br><div class='head'>");
    document.write(x[i].attributes.getNamedItem("head").value);
    document.write("</div><div class='copy'>");
    document.write(x[i].attributes.getNamedItem("desc").value);
    document.write("</div></a></td></tr></table></td>");
}

document.write("</tr>");

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

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要在for循环块内添加另一个增量器。

像这样:      

e = e++;

或者,您可以将for循环包裹在while循环中,使其成为无限循环,并使for循环停止在3.

如此:

var e = 0;
while (e < x.length){
for (i = 0; i < 3; i++) {
e = e + 1;
document.write("<td><table border='0' cellspacing='0' cellpadding='0' id='static' ><tr>
<td class='HOT969' colspan='3'><a href='");
document.write(x[i].attributes.getNamedItem("link").value);
document.write("'><img width='190' height='125' class='image' src='");
document.write(x[i].attributes.getNamedItem("image").value);
document.write("'><br><div class='head'>");
document.write(x[i].attributes.getNamedItem("head").value);
document.write("</div><div class='copy'>");
document.write(x[i].attributes.getNamedItem("desc").value);
document.write("</div></a></td></tr></table></td>");
}
}

document.write("</tr>");

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