Я делаю игру в крестики-нолики, где мне нужно создать кнопку, а затем добавить к ней события. В ряду три кнопки, в следующем ряду - 3 кнопки, а в следующем ряду - еще три кнопки. Я использовал тег прерывания, а также обозначение \ r \ n, но оно не работает. Пожалуйста помоги.
<body>
<div id = "displaytable"></div>
<script src='jquery-3.3.1.js'></script>
<script>
var table= [];
var blocks = 9;
var player,boardId;
winningCombinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
$(document).ready(function(){
buttonId = 0;
for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");
if ((index==2||index==5||index==8)&&(buttonId==3||buttonId==6||buttonId==9)){
button1.innerHTML = "\r\n"+"<br>";
}
button1.innerHTML = " + " ;
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);
button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
buttonId++;
}
});
</script>
</body>



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Ваше условие добавления br неверно, я добавил измененный код в js bin
var table= [];
var blocks = 9;
var player,boardId;
winningCombinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];
$(document).ready(function(){
buttonId = 0;
for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");
button1.innerHTML = " + " ;
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);
button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
if ((index==2||index==5||index==8)){
document.body.appendChild(document.createElement('br'));
}
buttonId++;
}
});<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "displaytable"></div>Вы можете сделать это с помощью CSS, отобразить кнопки в виде блоков и переместить их влево.
Очистить каждую третью кнопку.
var table = [];
var blocks = 9;
var player, boardId;
winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
$(document).ready(function() {
buttonId = 0;
for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");
if ((index == 2 || index == 5 || index == 8) && (buttonId == 3 || buttonId == 6 || buttonId == 9)) {
button1.innerHTML = "\r\n" + "<br>";
}
button1.innerHTML = " + ";
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);
button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
buttonId++;
}
}); button {
display: block;
float: left;
}
button:nth-of-type(3n+1) {
clear: both;
}<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "displaytable"></div>var table = [];
var blocks = 9;
var player, boardId;
winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
$(document).ready(function() {
buttonId = 0;
for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");
button1.innerHTML = " + ";
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);
button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
if ( (index+1) % 3 == 0 ) {
document.body.appendChild( document.createElement("br") );
}
buttonId++;
}
});<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id = "displaytable"></div>
Возможный дубликат: stackoverflow.com/questions/8147376/…