重复一个数组,但每次都有不同的字体属性

时间:2017-01-13 12:22:20

标签: javascript arrays

<body>

<p id="demo"></p>
 <script>
   var week = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"];
   var text = "";
   var i;
      for (i = 0; i < week.length; i++) {
      text += week[i] + "<br>";
      }

     document.getElementById("demo").innerHTML = text;
</script>
</body>

我目前有这个,它显示为

  • 星期一
  • theusday
  • 星期三

它只做了一次。它应该重复6次,但每次它应该将FONT的大小扩展为1,但文本内容本身应该保持不变。

3 个答案:

答案 0 :(得分:1)

你需要一个循环来做到这一点:

<body>

<p id="demo"></p>
 <script>
   var week = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"];
   var text = "";
   var i;
      for (var j = 1; j <= 6; j++) {
          text += "<h" + j + ">";
          for (i = 0; i < week.length; i++) {
              text += week[i] + "<br>";
          }
          text += "<h" + j + ">";
      }

     document.getElementById("demo").innerHTML = text;
</script>
</body>

答案 1 :(得分:0)

您只需将循环包装在另一个循环中以获取字体大小。

var weeks = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"];
var text = "";

for (let headerSize = 1; headerSize < 7; headerSize++) {
  text += `(Heading size: ${headerSize})`;
  text += weeks.map(week => `<h${headerSize}>${week}</h${headerSize}>`).join('\n');
}

document.getElementById("demo").innerHTML = text;
<div id="demo"></div>

答案 2 :(得分:0)

您也可以像这样增加样式font-size ..

var week = ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"];
var fontSize = 12;

for (var count = 0; count < 6; count++ ) {
  var text = "";
  var div = document.createElement('div');
  div.className = "days";
  div.style= "font-size:" + (fontSize + count + 1).toString() + "px;";
  for (var i = 0; i < week.length; i++) {
	  text += week[i] + "<br>";
  }
  div.innerHTML = text;
  document.getElementById("demo").appendChild(div);
}
div.days {
  padding-top:10px;
}
<p id="demo"></p>