2

我正在使用 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 张图片

4

4 回答 4

2
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
}
于 2012-05-14T14:57:35.560 回答
1

这可以解决问题:

var content = "", imgNum = 1, max = 4;

for (var i = 0; i < 4; i++) {
    if (i != 0) {
        max = 3;
    }
    for (var j = 0; j < max; j++) {
        content += "image" + imgNum;
        imgNum++;
    }
    content += "<br />";
}

document.write(content);

演示

这是逻辑:

  • 将起始图像编号设置为 1
  • 将最大值设置为 4(稍后我们将其更改为 3)
  • 循环行数(4次)
    • 如果我们在第一个循环,则保留最大值 4,否则将其更改为 3
    • 循环列数(最大次数)
      • 向内容添加内容
      • 将图像编号增加 1
    • 在循环之后添加中断

我更喜欢使用两个循环而不是使用%运算符,因为它更具可读性和更快。

编辑:如果您不关心性能,我找到了一种使用以下方法制作可读版本的方法%

var content="";

for (var i = 0; i < 13; i++){
    content += "image"+(i+1);
    if(i==0||i==12){
        continue;
    }
    if(i%3==0) {
        content += "<br />";
    }
}

document.write(content);
于 2012-05-14T15:13:15.683 回答
0

把它放在循环的末尾:

if (i == 3) { content += '<br/>'; }
if (i > 3 && (i - 4) % 3 == 2) { content += '<br/>'; }
于 2012-05-14T14:57:37.967 回答
0

那这个呢?

for (var i = 0; i < 13; i++){
    content += "<li>...</li>";
    if(i==3 || i==6 || i==9) { content += "<br />"; }
}
于 2012-05-14T14:59:44.237 回答