在嵌套循环的<p>标记中添加一行

时间:2015-12-23 12:26:39

标签: javascript jquery nested-loops

我需要在html页面中添加三角形,我还需要将每一行放在p tag中以检查其是偶数还是奇数

var line = '<p class = odd>'
  var l = " ";
  var myDiv = $('<div id = "container"></div>')
  $('body').append(myDiv)
  var randColor = '#' +(Math.random()*0xFFFFFF<<0).toString(16);

  for (var i = 1; i <= input; i++) {
    for (var j = 0; j <= input-i ; j++) {
    $('#container').css('color',randColor)

    $("#container").append( line + i);
    }
    $("#container").append(l);
  }

1 个答案:

答案 0 :(得分:0)

试试这个......

var line = '<p class = odd>'
var l = "</p>";
var myDiv = $('<div id = "container"></div>')
$('body').append(myDiv)
var randColor = '#' +(Math.random()*0xFFFFFF<<0).toString(16);
var str="";
for (var i = 1; i <= input; i++) {
  for (var j = 0; j <= input-i ; j++) {
     str+=i;
  }
}

$("#container").append( line + str+l);