Others have given you why this is happening, but I thought I could give an example of how you could make better use of css and more recent use of content.
Fiddle: http://jsfiddle.net/np62shu6/1/
But the main idea is to determine the number of cells, and then write out a series of divs with a 20% float value. As a result, you have a chessboard with a cell data attribute.
HTML:
<div id="game"> </div>
CSS
.cell{ width:20%; float:left; text-align:center; } .odd{ background:#eee; }
JS (assuming you put this in a load handler):
var cells = 25; var cell; var h; for(var i = 1; i <= cells; i ++) { cell = $('<div>').addClass('cell').attr('data-cell', i).text(i); if(i % 2 == 1) cell.addClass('odd'); $('#game').append(cell); } h = $('.cell:last-of-type').width(); $('.cell').css({height: h, lineHeight: h + 'px'});
As others have said, append is a sequential method, so calling it one by one will continue to clean things up in the DOM. But you can create elements and then add elements to these elements using append and then use append to add this group to another ...
My example does not show this. My example is just an alternative to what you wrote. I would not do it the way you do it, thatβs all.
Another small note - the chessboards have 64 cells (8 x 8), but I left them at 25 because your example did this.
Kai qing
source share