JavaScript For 循环如何工作?

标签 javascript for-loop

我正在使用 JavaScript for 循环,我的代码在这里

content = "";           
for (var i = 0; i < 13; i++) {
  var alt=glevel[i].getAttribute("name");
  if(jQuery.inArray(alt, clickArray) > -1) {
    if ($.browser.msie  && parseInt($.browser.version, 10) === 8 || $.browser.msie  && parseInt($.browser.version, 10) === 7) {
      content += "<li id='"+alt+"' style='cursor:pointer;filter:alpha(opacity=50);'>"+alt+"<br><div style='width:auto;'><img src='products/"+alt+".png'><br><font size='1px'>Click Base To Select</font></div><br><p style='text-decoration:none;color:#ffffff;font-size:10px;margin-top:-18px;' id='"+alt+"'>Click Here For Product Info</p></li> \n";
    } else {
      content += "<li id='"+alt+"' style='cursor:pointer;opacity:0.3;'>"+alt+"<br><div style='width:auto;'><img src='products/"+alt+".png'><br><font size='1px'>Click Base To Select</font></div><br><p style='text-decoration:none;color:#ffffff;font-size:10px;margin-top:-18px;' id='"+alt+"'>Click Here For Product Info</p></li> \n";
    }
    //$('#'+clickArray[alt]+"").css("opacity","0.5");
  } else{
    content += "<li id='"+alt+"' style='cursor:pointer'>"+alt+"<br><div style='width:auto;'><img src='products/"+alt+".png'><br><font size='1px'></font></div><br><p style='text-decoration:none;color:#ffffff;font-size:10px;margin-top:-18px;' id='"+alt+"'></p></li> \n";
  }
  $("#step_2").html(content);
}

这段代码的输出是这样的:-

image1 image2 image3 image4 
image5 image6 
image7 image8 image9 image10 
image11 image12 image13

update:- it looks like that because of my table width and height

它工作得很好,它显示了我拥有的真实产品图像。

现在我想显示这样的东西:-

image1 image2 image3 image4     
image5 image6 image7     
image8 image9 image10     
image11 image12 image13

表示第一行有4张图片,而第2、3、4行有3张图片

最佳答案

for (var i = 0; i < 13; i++) {
    content += "...";

    if (i == 3)                       // after the fourth line,
       content += '<br/><br/>';       // add an empty line
    if (i > 3 && (i - 4) % 3 == 2)    // then, after every three lines
       content += '<br/><br/>';       // add an empty line
}

关于JavaScript For 循环如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585745/

相关文章:

java - 我在 Swift 和 Java 中的代码。 Swift 给出错误,但 Java 没有。有什么不同吗?

python - 在循环 python 中查找下一个迭代

java - 如何打破方法中的循环

python - 使用 For 循环查找序列表达式 [1/1+1/2+1/3...1/1000]

python - 对一组数字进行编码时遇到麻烦

javascript - 即使添加了很多延迟后,焦点也无法在 IE 中工作

javascript - 唯一识别传单标记

javascript - 当用户从 ul 元素中选择项目时如何触发函数?

javascript - 在js中不使用htaccess将搜索从/?s=searchterm更改为/searchterm

php - 实时 Ajax 搜索不打印所有结果